Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip missing scripts(test or build) #465

Merged
merged 2 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion packages/shipjs/src/step/release/__tests__/runBuild.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import runBuild from '../runBuild';
import { run } from '../../../util';
import { run, print } from '../../../util';

describe('runBuild', () => {
it('works', () => {
Expand All @@ -19,4 +19,48 @@ describe('runBuild', () => {
}
`);
});

it('skips build if buildCommand is falsy', () => {
runBuild({
config: {
buildCommand: null,
},
dir: '.',
dryRun: false,
});
expect(run).toHaveBeenCalledTimes(0);
expect(print).toHaveBeenCalledTimes(2);
expect(print.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"› Building.",
],
Array [
"Skipping build because it is not configured.",
],
]
`);
});

it('skips build if buildCommand returns falsy', () => {
runBuild({
config: {
buildCommand: () => '',
},
dir: '.',
dryRun: false,
});
expect(run).toHaveBeenCalledTimes(0);
expect(print).toHaveBeenCalledTimes(2);
expect(print.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"› Building.",
],
Array [
"Skipping build because it is not configured.",
],
]
`);
});
});
46 changes: 45 additions & 1 deletion packages/shipjs/src/step/release/__tests__/runTest.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import runTest from '../runTest';
import { run } from '../../../util';
import { run, print } from '../../../util';

describe('runTest', () => {
it('works', () => {
Expand All @@ -19,4 +19,48 @@ describe('runTest', () => {
}
`);
});

it('skips test if testCommandBeforeRelease is falsy', () => {
runTest({
config: {
testCommandBeforeRelease: null,
},
dir: '.',
dryRun: false,
});
expect(run).toHaveBeenCalledTimes(0);
expect(print).toHaveBeenCalledTimes(2);
expect(print.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"› Running test.",
],
Array [
"Skipping test because it is not configured.",
],
]
`);
});

it('skips test if testCommandBeforeRelease returns falsy', () => {
runTest({
config: {
testCommandBeforeRelease: () => '',
},
dir: '.',
dryRun: false,
});
expect(run).toHaveBeenCalledTimes(0);
expect(print).toHaveBeenCalledTimes(2);
expect(print.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"› Running test.",
],
Array [
"Skipping test because it is not configured.",
],
]
`);
});
});
10 changes: 8 additions & 2 deletions packages/shipjs/src/step/release/runBuild.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import runStep from '../runStep';
import { run } from '../../util';
import { run, print } from '../../util';
import { warning } from '../../color';

export default ({ isYarn, config, dir, dryRun }) =>
runStep({ title: 'Building.' }, () => {
const { buildCommand } = config;
run({ command: buildCommand({ isYarn }), dir, dryRun });
const command = buildCommand && buildCommand({ isYarn });
if (!command) {
print(warning('Skipping build because it is not configured.'));
return;
}
run({ command, dir, dryRun });
});
11 changes: 9 additions & 2 deletions packages/shipjs/src/step/release/runTest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import runStep from '../runStep';
import { run } from '../../util';
import { run, print } from '../../util';
import { warning } from '../../color';

export default ({ isYarn, config, dir, dryRun }) =>
runStep({ title: 'Running test.' }, () => {
const { testCommandBeforeRelease } = config;
run({ command: testCommandBeforeRelease({ isYarn }), dir, dryRun });
const command =
testCommandBeforeRelease && testCommandBeforeRelease({ isYarn });
if (!command) {
print(warning('Skipping test because it is not configured.'));
return;
}
run({ command, dir, dryRun });
});
43 changes: 29 additions & 14 deletions packages/shipjs/src/step/setup/addShipConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export default async ({
dir,
}) =>
await runStep({ title: 'Creating ship.config.js' }, async () => {
const filePath = path.resolve(dir, 'ship.config.js');
const json = {
publishCommand:
isScoped && isPublic
? ({ defaultCommand }) => `${defaultCommand} --access public`
: undefined,
const { testExists, buildExists } = checkIfScriptsExist({ dir });
const config = {
...(isScoped &&
isPublic && {
publishCommand: ({ defaultCommand }) =>
`${defaultCommand} --access public`,
}),
mergeStrategy:
baseBranch === releaseBranch
? {
Expand All @@ -34,16 +35,19 @@ export default async ({
[baseBranch]: releaseBranch,
},
},
monorepo: useMonorepo
? {
mainVersionFile,
packagesToBump,
packagesToPublish,
}
: undefined,
...(useMonorepo && {
monorepo: {
mainVersionFile,
packagesToBump,
packagesToPublish,
},
}),
...(!testExists && { testCommandBeforeRelease: () => null }),
...(!buildExists && { buildCommand: () => null }),
};

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

return () => {
Expand All @@ -52,3 +56,14 @@ export default async ({
print(' > https://github.com/algolia/shipjs/blob/master/GUIDE.md');
};
});

function checkIfScriptsExist({ dir }) {
const filePath = path.resolve(dir, 'package.json');
const json = JSON.parse(fs.readFileSync(filePath).toString());
const { test, build } = json.scripts || {};
return {
testExists:
Boolean(test) && test !== 'echo "Error: no test specified" && exit 1',
buildExists: Boolean(build),
};
}