Skip to content

Instantly share code, notes, and snippets.

@qodunpob
Last active August 2, 2018 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qodunpob/8f990dba6c09be695fc3d86ca96da862 to your computer and use it in GitHub Desktop.
Save qodunpob/8f990dba6c09be695fc3d86ca96da862 to your computer and use it in GitHub Desktop.
import { spawnSync } from 'child_process';
import { mkdirSync, existsSync } from 'fs';
import { join } from 'path';
import chalk from 'chalk';
/**
* Module publication script
*/
/**
* Generating parameters for subprocesses
* @param cwd — subprocess directory
* @param stdio — input/output settings
*/
const getSpawnOptions = (cwd = process.cwd(), stdio = 'inherit') => ({
cwd,
shell: true,
stdio,
});
/* module directory root */
const rootDir = join(__dirname, '../');
/* checking if there are any uncommitted changes */
const isDiff = !!spawnSync('git', ['diff'], getSpawnOptions(rootDir, 'pipe')).stdout.toString().trim();
if (isDiff) {
console.log(chalk.red('There are uncommitted changes'));
} else {
/* project build*/
const build = spawnSync('npm', ['run', 'build'], getSpawnOptions(rootDir));
/* checking build status */
if (build.status === 0) {
/* temporary directory for the builds repository */
const tempDir = join(rootDir, 'temp');
if (existsSync(tempDir)) {
spawnSync('rm', ['-rf', 'temp'], getSpawnOptions(rootDir));
}
mkdirSync(tempDir);
/* getting parameters from the package.json */
const { name, version, repository } = require(join(rootDir, 'package.json'));
const originUrl = repository.url.replace(`${name}-source`, name);
spawnSync('git', ['init'], getSpawnOptions(tempDir));
spawnSync('git', ['remote', 'add', 'origin', originUrl], getSpawnOptions(tempDir));
/* current branch name in the module source code repository */
const branch = spawnSync(
'git',
['symbolic-ref', '--short', 'HEAD'],
getSpawnOptions(rootDir, 'pipe')
).stdout.toString().trim();
/* branch name in the builds repository */
const buildBranch = branch === 'develop' ? 'master' : branch;
/* Shortened hash of the last commit in source code repository.
Used for forming of the unstable version tag */
const shortSHA = spawnSync(
'git',
['rev-parse', '--short', 'HEAD'],
getSpawnOptions(rootDir, 'pipe')
).stdout.toString().trim();
const tag = buildBranch === 'master' ? version : `${version}_${shortSHA}`;
/* checking if the created tag exists in build repository */
const isTagExists = !!spawnSync(
'git',
['ls-remote', 'origin', `refs/tags/${tag}`],
getSpawnOptions(tempDir, 'pipe')
).stdout.toString().trim();
if (isTagExists) {
console.log(chalk.red(`Tag ${tag} already exists`));
} else {
/* checking if branch exists in builds repository */
const isBranchExits = !!spawnSync(
'git',
['ls-remote', '--exit-code', 'origin', buildBranch],
getSpawnOptions(tempDir, 'pipe')
).stdout.toString().trim();
if (isBranchExits) {
/* checkout the target branch */
spawnSync('git', ['fetch', 'origin', buildBranch], getSpawnOptions(tempDir));
spawnSync('git', ['checkout', buildBranch], getSpawnOptions(tempDir));
} else {
/* checkout master */
spawnSync('git', ['fetch', 'origin', 'master'], getSpawnOptions(tempDir));
spawnSync('git', ['checkout', 'master'], getSpawnOptions(tempDir));
/* creating branch */
spawnSync('git', ['checkout', '-b', buildBranch], getSpawnOptions(tempDir));
/* creating initial commit */
spawnSync('git', ['commit', '--allow-empty', '-m', '"Initial commit"'], getSpawnOptions(tempDir));
}
/* removing the old build files */
spawnSync(
'rm',
['-rf', 'lib', 'package.json', 'package-lock.json', 'README.md'],
getSpawnOptions(tempDir)
);
/* creating build files copies */
spawnSync('cp', ['-r', 'lib', 'temp/lib'], getSpawnOptions(rootDir));
spawnSync('cp', ['package.json', 'temp/package.json'], getSpawnOptions(rootDir));
spawnSync('cp', ['package-lock.json', 'temp/package-lock.json'], getSpawnOptions(rootDir));
spawnSync('cp', ['README.md', 'temp/README.md'], getSpawnOptions(rootDir));
/* indexing the build files */
spawnSync('git', ['add', '--all'], getSpawnOptions(tempDir));
/* message of the last commit in source code repository */
const lastCommitMessage = spawnSync(
'git',
['log', '--oneline', '-1'],
getSpawnOptions(rootDir, 'pipe')
).stdout.toString().trim();
/* commit message in build repository */
const message = buildBranch === 'master' ? version : lastCommitMessage;
/* making commit in builds repository */
spawnSync('git', ['commit', '-m', `”${message}”`], getSpawnOptions(tempDir));
/* creating a tag in builds repository */
spawnSync('git', ['tag', tag], getSpawnOptions(tempDir));
/* pushing changes to the remote repository */
spawnSync('git', ['push', 'origin', buildBranch], getSpawnOptions(tempDir));
spawnSync('git', ['push', '--tags'], getSpawnOptions(tempDir));
console.log(chalk.green('Published successfully!'));
}
/* removing temporary directory */
spawnSync('rm', ['-rf', 'temp'], getSpawnOptions(rootDir));
} else {
console.log(chalk.red(`Build was exited exited with code ${build.status}`));
}
}
console.log(''); // space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment