Skip to content

Commit

Permalink
feat(setup): add --dry-run flag (#492)
Browse files Browse the repository at this point in the history
* add dry-fun flag for setup step

* add help message for --dry-run option

* fix: update test snapshot
  • Loading branch information
jeetiss authored and Eunjae Lee committed Dec 4, 2019
1 parent 2b32dfd commit 1beb810
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 23 deletions.
14 changes: 11 additions & 3 deletions packages/shipjs/src/flow/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import printHelp from '../step/setup/printHelp';
import printDryRunBanner from '../step/printDryRunBanner';
import askQuestions from '../step/setup/askQuestions';
import addDevDependencies from '../step/setup/addDevDependencies';
import addScriptsToPackageJson from '../step/setup/addScriptsToPackageJson';
Expand All @@ -7,11 +8,14 @@ import addCircleCIConfig from '../step/setup/addCircleCIConfig';
import { print } from '../util';
import { success } from '../color';

async function setup({ help = false, dir = '.' }) {
async function setup({ help = false, dir = '.', dryRun = false }) {
if (help) {
printHelp();
return;
}
if (dryRun) {
printDryRunBanner();
}
const {
baseBranch,
releaseBranch,
Expand All @@ -26,8 +30,8 @@ async function setup({ help = false, dir = '.' }) {
isPublic,
} = await askQuestions({ dir });
const outputs = [
addDevDependencies({ dependencies: ['shipjs'], dir }),
addScriptsToPackageJson({ dir }),
addDevDependencies({ dependencies: ['shipjs'], dir, dryRun }),
await addScriptsToPackageJson({ dir, dryRun }),
await addShipConfig({
isScoped,
isPublic,
Expand All @@ -38,13 +42,15 @@ async function setup({ help = false, dir = '.' }) {
packagesToBump,
packagesToPublish,
dir,
dryRun,
}),
addCircleCIConfig({
baseBranch,
configureCircleCI,
scheduleCircleCI,
cronExpr,
dir,
dryRun,
}),
];

Expand All @@ -61,10 +67,12 @@ async function setup({ help = false, dir = '.' }) {
const arg = {
'--dir': String,
'--help': Boolean,
'--dry-run': Boolean,

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

export default {
Expand Down
5 changes: 4 additions & 1 deletion packages/shipjs/src/step/setup/__tests__/printHelp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ describe('printHelp', () => {
shipjs setup - Setup Ship.js in your project.
USAGE
npx shipjs setup [--help] [--dir PATH]
npx shipjs setup [--help] [--dir PATH] [--dry-run]
OPTIONS
-h, --help
Print this help
-d, --dir PATH
Specify the PATH of the repository (default: the current directory).
-D, --dry-run
Displays the steps without actually doing them.
",
]
`);
Expand Down
10 changes: 8 additions & 2 deletions packages/shipjs/src/step/setup/addCircleCIConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default ({
scheduleCircleCI,
cronExpr,
dir,
dryRun,
}) =>
runStep(
{
Expand Down Expand Up @@ -42,8 +43,13 @@ export default ({
gitUserName: getGitConfig('user.name') || 'Your Name',
gitUserEmail: getGitConfig('user.email') || '[email protected]',
});
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, content);
if (dryRun) {
print(`.circleci/config.yml`);
print(content);
} else {
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, content);
}
return () => {
print(`${info('✔')} Created \`.circleci/config.yml\`.`);
print(' You still need to finish setting up at CircleCI.');
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/step/setup/addDevDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { info } from '../../color';
import fs from 'fs';
import path from 'path';

export default ({ dependencies, dir }) =>
export default ({ dependencies, dir, dryRun }) =>
runStep({ title: 'Installing Ship.js' }, () => {
const command = detectYarn(dir)
? `yarn add --exact --dev ${dependencies.join(' ')}${
usesYarnWorkspace(dir) ? ' -W' : ''
}`
: `npm install --save-exact --save-dev ${dependencies.join(' ')}`;
run({ command, dir, silent: true, printCommand: false });
run({ command, dir, silent: true, printCommand: dryRun, dryRun });
return () => print(`${info('✔')} Installed shipjs as devDependency.`);
});

Expand Down
20 changes: 11 additions & 9 deletions packages/shipjs/src/step/setup/addScriptsToPackageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import { runPrettier } from '../../helper';
import { info } from '../../color';
import { print } from '../../util';

export default ({ dir }) =>
runStep({ title: 'Adding scripts to package.json' }, () => {
const filePath = path.resolve(dir, 'package.json');
const json = JSON.parse(fs.readFileSync(filePath).toString());
json.scripts = json.scripts || {};
json.scripts['release:prepare'] = 'shipjs prepare';
json.scripts['release:trigger'] = 'shipjs trigger';
fs.writeFileSync(filePath, `${JSON.stringify(json, null, 2)}\n`);
runPrettier({ filePath, dir });
export default async ({ dir, dryRun }) =>
await runStep({ title: 'Adding scripts to package.json' }, async () => {
if (!dryRun) {
const filePath = path.resolve(dir, 'package.json');
const json = JSON.parse(fs.readFileSync(filePath).toString());
json.scripts = json.scripts || {};
json.scripts['release:prepare'] = 'shipjs prepare';
json.scripts['release:trigger'] = 'shipjs trigger';
fs.writeFileSync(filePath, `${JSON.stringify(json, null, 2)}\n`);
await runPrettier({ filePath, dir });
}
return () =>
print(
`${info(
Expand Down
13 changes: 9 additions & 4 deletions packages/shipjs/src/step/setup/addShipConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default async ({
packagesToBump,
packagesToPublish,
dir,
dryRun,
}) =>
await runStep({ title: 'Creating ship.config.js' }, async () => {
const { testExists, buildExists } = checkIfScriptsExist({ dir });
Expand Down Expand Up @@ -45,10 +46,14 @@ export default async ({
...(!testExists && { testCommandBeforeRelease: () => null }),
...(!buildExists && { buildCommand: () => null }),
};

const filePath = path.resolve(dir, 'ship.config.js');
fs.writeFileSync(filePath, `module.exports = ${serialize(config)};`);
await runPrettier({ filePath, dir });
if (dryRun) {
print(`ship.config.js`);
print(serialize(config));
} else {
const filePath = path.resolve(dir, 'ship.config.js');
fs.writeFileSync(filePath, `module.exports = ${serialize(config)};`);
await runPrettier({ filePath, dir });
}

return () => {
print(`${info('✔')} Created \`ship.config.js\`.`);
Expand Down
7 changes: 5 additions & 2 deletions packages/shipjs/src/step/setup/printHelp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { bold, underline } from '../../color';
export default () =>
runStep({}, () => {
const indent = line => `\t${line}`;

const help = `--help`;
const dir = `--dir ${underline('PATH')}`;
const all = [help, dir].map(x => `[${x}]`).join(' ');
const dryRun = `--dry-run`;
const all = [help, dir, dryRun].map(x => `[${x}]`).join(' ');

const messages = [
bold('NAME'),
Expand All @@ -28,6 +28,9 @@ export default () =>
)} of the repository (default: the current directory).`
),
'',
indent(`-D, ${dryRun}`),
indent(' Displays the steps without actually doing them.'),
'',
];
print(messages.join('\n'));
});

0 comments on commit 1beb810

Please sign in to comment.