Skip to content

Commit

Permalink
feat(setup): add npx shipjs setup (#361)
Browse files Browse the repository at this point in the history
* feat: setup WIP

* feat: ask questions WIP

* feat: add steps for setup WIP

* feat: add steps for setup
  • Loading branch information
Eunjae Lee authored Oct 24, 2019
1 parent 27afde7 commit df70f0a
Show file tree
Hide file tree
Showing 27 changed files with 748 additions and 27 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"tw1": "yarn workspace shipjs-lib test:watch",
"tw2": "yarn workspace shipjs test:watch",
"bootstrap": "./packages/shipjs-lib/tests/bootstrap.sh",
"release:prepare": "npx shipjs prepare",
"release:trigger": "npx shipjs trigger",
"release:prepare": "shipjs prepare",
"release:trigger": "shipjs trigger",
"toc": "npx markdown-toc -i --bullets=\"-\" GUIDE.md",
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate"
Expand All @@ -30,4 +30,4 @@
"all-contributors-cli": "6.9.3",
"lerna": "3.18.3"
}
}
}
2 changes: 2 additions & 0 deletions packages/shipjs-lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export { default as getLatestCommitHash } from './lib/git/getLatestCommitHash';
export { default as getCommitUrl } from './lib/git/getCommitUrl';
export { default as isWorkingTreeClean } from './lib/git/isWorkingTreeClean';
export { default as hasTag } from './lib/git/hasTag';
export { default as getRemoteBranches } from './lib/git/getRemoteBranches';
export { default as getGitConfig } from './lib/git/getGitConfig';

/* shell */
export { default as exec } from './lib/shell/exec';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import silentExec from '../../shell/silentExec';
import getRemoteBranches from '../getRemoteBranches';
jest.mock('../../shell/silentExec');

describe('getRemoteBranches', () => {
it('works', () => {
silentExec
.mockImplementationOnce(() => 'origin')
.mockImplementationOnce(
() => ` origin/HEAD -> origin/master
origin/chore/all-contributors
origin/master
origin/renovate/pin-dependencies
origin/renovate/rollup-1.x
`
);
const result = getRemoteBranches();
expect(result).toEqual([
'chore/all-contributors',
'master',
'renovate/pin-dependencies',
'renovate/rollup-1.x',
]);
});
});
7 changes: 7 additions & 0 deletions packages/shipjs-lib/src/lib/git/getGitConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import silentExec from '../shell/silentExec';

export default function getGitConfig(key, dir = '.') {
return silentExec(`git config ${key}`, { dir, ignoreError: true })
.toString()
.trim();
}
24 changes: 24 additions & 0 deletions packages/shipjs-lib/src/lib/git/getRemoteBranches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import silentExec from '../shell/silentExec';

/*
$ git branch -r
origin/HEAD -> origin/master
origin/chore/all-contributors
origin/master
origin/renovate/pin-dependencies
origin/renovate/rollup-1.x
...
*/

export default function getRemoteBranches(dir = '.') {
const remote = silentExec('git remote', { dir })
.toString()
.trim();
return silentExec('git branch -r', { dir })
.toString()
.trim()
.split('\n')
.map(line => line.trim())
.filter(line => !line.includes(' -> '))
.map(line => line.slice(remote.length + 1));
}
12 changes: 10 additions & 2 deletions packages/shipjs/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { info, warning, error, slateblue, bold, underline } from './src/color';
import {
info,
warning,
error,
slateblue,
bold,
underline,
reset,
} from './src/color';
import { mockColor } from './tests/util';
jest.mock('shipjs-lib');
jest.mock('./src/color');
jest.mock('./src/util');
jest.mock('./src/helper');

beforeEach(() => {
[info, warning, error, slateblue, bold, underline].forEach(mockColor);
[info, warning, error, slateblue, bold, underline, reset].forEach(mockColor);
});

afterEach(() => {
Expand Down
6 changes: 5 additions & 1 deletion packages/shipjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"test:run": "shipjs",
"test:run:version": "shipjs --version",
"test:run:setup": "shipjs setup",
"test:run:prepare": "shipjs prepare",
"test:run:trigger": "shipjs trigger",
"test": "jest",
Expand Down Expand Up @@ -38,9 +39,12 @@
"chalk": "2.4.2",
"change-case": "3.1.0",
"conventional-changelog-cli": "2.0.25",
"ejs": "^2.7.1",
"esm": "3.2.25",
"globby": "^10.0.1",
"inquirer": "7.0.0",
"mkdirp": "^0.5.1",
"prettier": "^1.18.2",
"shell-quote": "^1.7.2",
"shipjs-lib": "0.7.1",
"temp-write": "4.0.0"
Expand All @@ -61,4 +65,4 @@
"jest": "24.9.0",
"jest-watch-typeahead": "0.4.0"
}
}
}
5 changes: 5 additions & 0 deletions packages/shipjs/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import setup from './flow/setup';
import prepare from './flow/prepare';
import release from './flow/release';
import parseArgs from 'arg';
Expand All @@ -7,6 +8,7 @@ import { print } from './util';
import version from './version';

const flowMap = {
setup,
prepare,
trigger: release,
};
Expand All @@ -26,6 +28,9 @@ function printVersion() {

function printHelp() {
print(bold('USAGE'));
print(`\t${bold('shipjs setup')} --help`);
print(`\t : Setup Ship.js in your project.`);
print('');
print(`\t${bold('shipjs prepare')} --help`);
print(`\t : Prepare a release.`);
print('');
Expand Down
1 change: 1 addition & 0 deletions packages/shipjs/src/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const error = chalk.keyword('tomato');
export const slateblue = chalk.keyword('slateblue');
export const bold = chalk.bold;
export const underline = chalk.underline;
export const reset = chalk.reset;
5 changes: 3 additions & 2 deletions packages/shipjs/src/flow/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ async function prepare({
const config = loadConfig(dir);
const { currentVersion, baseBranch } = validate({ config, dir });
validateMergeStrategy({ config });
pull({ dir, dryRun });
push({ config, currentBranch: baseBranch, dir, dryRun });
const { remote } = config;
pull({ remote, currentBranch: baseBranch, dir, dryRun });
push({ remote, currentBranch: baseBranch, dir, dryRun });
let { nextVersion } = getNextVersion({ currentVersion, dir });
nextVersion = await confirmNextVersion({
yes,
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/flow/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import printDryRunBanner from '../step/printDryRunBanner';
import validate from '../step/release/validate';
import gatherRepoInfo from '../step/release/gatherRepoInfo';
import notifyReleaseStart from '../step/release/notifyReleaseStart';
import detectYarn from '../step/detectYarn';
import runTest from '../step/release/runTest';
import runBuild from '../step/release/runBuild';
import runBeforePublish from '../step/release/runBeforePublish';
Expand All @@ -16,6 +15,7 @@ import gitPush from '../step/release/gitPush';
import createGitHubRelease from '../step/release/createGitHubRelease';
import notifyReleaseSuccess from '../step/release/notifyReleaseSuccess';
import finished from '../step/finished';
import { detectYarn } from '../util';

async function release({ help = false, dir = '.', dryRun = false }) {
if (help) {
Expand All @@ -42,7 +42,7 @@ async function release({ help = false, dir = '.', dryRun = false }) {
latestCommitHash,
latestCommitUrl,
});
const isYarn = detectYarn({ dir });
const isYarn = detectYarn(dir);
runTest({ isYarn, config, dir, dryRun });
runBuild({ isYarn, config, dir, dryRun });
await runBeforePublish({ config, dir, dryRun });
Expand Down
56 changes: 56 additions & 0 deletions packages/shipjs/src/flow/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import printHelp from '../step/setup/printHelp';
import askQuestions from '../step/setup/askQuestions';
import addDevDependencies from '../step/setup/addDevDependencies';
import addScriptsToPackageJson from '../step/setup/addScriptsToPackageJson';
import addShipConfig from '../step/setup/addShipConfig';
import addCircleCIConfig from '../step/setup/addCircleCIConfig';

async function setup({ help = false, dir = '.' }) {
if (help) {
printHelp();
return;
}
const {
baseBranch,
releaseBranch,
configureCircleCI,
scheduleCircleCI,
cronExpr,
useMonorepo,
mainVersionFile,
packagesToBump,
packagesToPublish,
} = await askQuestions({ dir });
addDevDependencies({ dependencies: ['shipjs'], dir });
addScriptsToPackageJson({ dir });
await addShipConfig({
baseBranch,
releaseBranch,
useMonorepo,
mainVersionFile,
packagesToBump,
packagesToPublish,
dir,
});
addCircleCIConfig({
baseBranch,
configureCircleCI,
scheduleCircleCI,
cronExpr,
dir,
});
}

const arg = {
'--dir': String,
'--help': Boolean,

// Aliases
'-d': '--dir',
'-h': '--help',
};

export default {
arg,
fn: setup,
};
1 change: 1 addition & 0 deletions packages/shipjs/src/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { default as getChangelog } from './getChangelog';
export { default as gitPush } from './gitPush';
export { default as hubInstalled } from './hubInstalled';
export { default as hubConfigured } from './hubConfigured';
export { default as runPrettier } from './runPrettier';
21 changes: 21 additions & 0 deletions packages/shipjs/src/helper/runPrettier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import prettier from 'prettier';
import fs from 'fs';
import path from 'path';

export default async function runPrettier({ filePath, dir }) {
const text = fs.readFileSync(filePath).toString();
const options = await prettier.resolveConfig(dir);
const formatted = prettier.format(text, {
parser: getParser(filePath),
...(options || {}),
});
fs.writeFileSync(filePath, formatted);
}

function getParser(filePath) {
const parser = {
'.json': 'json',
'.js': 'babel',
}[path.extname(filePath)];
return parser;
}
7 changes: 0 additions & 7 deletions packages/shipjs/src/step/detectYarn.js

This file was deleted.

3 changes: 1 addition & 2 deletions packages/shipjs/src/step/prepare/push.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import runStep from '../runStep';
import { gitPush } from '../../helper';

export default ({ config, currentBranch, dir, dryRun }) =>
export default ({ remote, currentBranch, dir, dryRun }) =>
runStep({ title: 'Pushing to remote.' }, () => {
const { remote } = config;
gitPush({ remote, refs: [currentBranch], dir, dryRun });
});
4 changes: 2 additions & 2 deletions packages/shipjs/src/step/pull.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import runStep from './runStep';
import { run } from '../util';

export default ({ dir, dryRun }) =>
export default ({ remote, currentBranch, dir, dryRun }) =>
runStep({ title: 'Updating from remote.' }, () => {
run({ command: 'git pull', dir, dryRun });
run({ command: `git pull ${remote} ${currentBranch}`, dir, dryRun });
});
6 changes: 3 additions & 3 deletions packages/shipjs/src/step/runStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { print } from '../util';
import { bold, slateblue } from '../color';

export default function runStep({ title, skipIf }, stepFn) {
if (title) {
print(bold(slateblue(`› ${title}`)));
}
if (skipIf && typeof skipIf === 'function' && skipIf() === true) {
return null;
}
if (title) {
print(bold(slateblue(`› ${title}`)));
}
return stepFn();
}
28 changes: 28 additions & 0 deletions packages/shipjs/src/step/setup/__tests__/printHelp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { print } from '../../../util';
import printHelp from '../printHelp';
import { mockPrint } from '../../../../tests/util';

describe('printHelp', () => {
it('prints help', () => {
const output = [];
mockPrint(print, output);
printHelp();
expect(output).toMatchInlineSnapshot(`
Array [
"NAME
shipjs setup - Setup Ship.js in your project.
USAGE
npx shipjs setup [--help] [--dir PATH]
OPTIONS
-h, --help
Print this help
-d, --dir PATH
Specify the PATH of the repository (default: the current directory).
",
]
`);
});
});
Loading

0 comments on commit df70f0a

Please sign in to comment.