Skip to content

Commit 51a58a4

Browse files
author
Eunjae Lee
authored
fix: skip missing scripts(test or build) (#465)
1 parent 928c28b commit 51a58a4

File tree

5 files changed

+136
-20
lines changed

5 files changed

+136
-20
lines changed

packages/shipjs/src/step/release/__tests__/runBuild.spec.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import runBuild from '../runBuild';
2-
import { run } from '../../../util';
2+
import { run, print } from '../../../util';
33

44
describe('runBuild', () => {
55
it('works', () => {
@@ -19,4 +19,48 @@ describe('runBuild', () => {
1919
}
2020
`);
2121
});
22+
23+
it('skips build if buildCommand is falsy', () => {
24+
runBuild({
25+
config: {
26+
buildCommand: null,
27+
},
28+
dir: '.',
29+
dryRun: false,
30+
});
31+
expect(run).toHaveBeenCalledTimes(0);
32+
expect(print).toHaveBeenCalledTimes(2);
33+
expect(print.mock.calls).toMatchInlineSnapshot(`
34+
Array [
35+
Array [
36+
"› Building.",
37+
],
38+
Array [
39+
"Skipping build because it is not configured.",
40+
],
41+
]
42+
`);
43+
});
44+
45+
it('skips build if buildCommand returns falsy', () => {
46+
runBuild({
47+
config: {
48+
buildCommand: () => '',
49+
},
50+
dir: '.',
51+
dryRun: false,
52+
});
53+
expect(run).toHaveBeenCalledTimes(0);
54+
expect(print).toHaveBeenCalledTimes(2);
55+
expect(print.mock.calls).toMatchInlineSnapshot(`
56+
Array [
57+
Array [
58+
"› Building.",
59+
],
60+
Array [
61+
"Skipping build because it is not configured.",
62+
],
63+
]
64+
`);
65+
});
2266
});

packages/shipjs/src/step/release/__tests__/runTest.spec.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import runTest from '../runTest';
2-
import { run } from '../../../util';
2+
import { run, print } from '../../../util';
33

44
describe('runTest', () => {
55
it('works', () => {
@@ -19,4 +19,48 @@ describe('runTest', () => {
1919
}
2020
`);
2121
});
22+
23+
it('skips test if testCommandBeforeRelease is falsy', () => {
24+
runTest({
25+
config: {
26+
testCommandBeforeRelease: null,
27+
},
28+
dir: '.',
29+
dryRun: false,
30+
});
31+
expect(run).toHaveBeenCalledTimes(0);
32+
expect(print).toHaveBeenCalledTimes(2);
33+
expect(print.mock.calls).toMatchInlineSnapshot(`
34+
Array [
35+
Array [
36+
"› Running test.",
37+
],
38+
Array [
39+
"Skipping test because it is not configured.",
40+
],
41+
]
42+
`);
43+
});
44+
45+
it('skips test if testCommandBeforeRelease returns falsy', () => {
46+
runTest({
47+
config: {
48+
testCommandBeforeRelease: () => '',
49+
},
50+
dir: '.',
51+
dryRun: false,
52+
});
53+
expect(run).toHaveBeenCalledTimes(0);
54+
expect(print).toHaveBeenCalledTimes(2);
55+
expect(print.mock.calls).toMatchInlineSnapshot(`
56+
Array [
57+
Array [
58+
"› Running test.",
59+
],
60+
Array [
61+
"Skipping test because it is not configured.",
62+
],
63+
]
64+
`);
65+
});
2266
});
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import runStep from '../runStep';
2-
import { run } from '../../util';
2+
import { run, print } from '../../util';
3+
import { warning } from '../../color';
34

45
export default ({ isYarn, config, dir, dryRun }) =>
56
runStep({ title: 'Building.' }, () => {
67
const { buildCommand } = config;
7-
run({ command: buildCommand({ isYarn }), dir, dryRun });
8+
const command = buildCommand && buildCommand({ isYarn });
9+
if (!command) {
10+
print(warning('Skipping build because it is not configured.'));
11+
return;
12+
}
13+
run({ command, dir, dryRun });
814
});
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import runStep from '../runStep';
2-
import { run } from '../../util';
2+
import { run, print } from '../../util';
3+
import { warning } from '../../color';
34

45
export default ({ isYarn, config, dir, dryRun }) =>
56
runStep({ title: 'Running test.' }, () => {
67
const { testCommandBeforeRelease } = config;
7-
run({ command: testCommandBeforeRelease({ isYarn }), dir, dryRun });
8+
const command =
9+
testCommandBeforeRelease && testCommandBeforeRelease({ isYarn });
10+
if (!command) {
11+
print(warning('Skipping test because it is not configured.'));
12+
return;
13+
}
14+
run({ command, dir, dryRun });
815
});

packages/shipjs/src/step/setup/addShipConfig.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ export default async ({
1818
dir,
1919
}) =>
2020
await runStep({ title: 'Creating ship.config.js' }, async () => {
21-
const filePath = path.resolve(dir, 'ship.config.js');
22-
const json = {
23-
publishCommand:
24-
isScoped && isPublic
25-
? ({ defaultCommand }) => `${defaultCommand} --access public`
26-
: undefined,
21+
const { testExists, buildExists } = checkIfScriptsExist({ dir });
22+
const config = {
23+
...(isScoped &&
24+
isPublic && {
25+
publishCommand: ({ defaultCommand }) =>
26+
`${defaultCommand} --access public`,
27+
}),
2728
mergeStrategy:
2829
baseBranch === releaseBranch
2930
? {
@@ -34,16 +35,19 @@ export default async ({
3435
[baseBranch]: releaseBranch,
3536
},
3637
},
37-
monorepo: useMonorepo
38-
? {
39-
mainVersionFile,
40-
packagesToBump,
41-
packagesToPublish,
42-
}
43-
: undefined,
38+
...(useMonorepo && {
39+
monorepo: {
40+
mainVersionFile,
41+
packagesToBump,
42+
packagesToPublish,
43+
},
44+
}),
45+
...(!testExists && { testCommandBeforeRelease: () => null }),
46+
...(!buildExists && { buildCommand: () => null }),
4447
};
4548

46-
fs.writeFileSync(filePath, `module.exports = ${serialize(json)};`);
49+
const filePath = path.resolve(dir, 'ship.config.js');
50+
fs.writeFileSync(filePath, `module.exports = ${serialize(config)};`);
4751
await runPrettier({ filePath, dir });
4852

4953
return () => {
@@ -52,3 +56,14 @@ export default async ({
5256
print(' > https://github.com/algolia/shipjs/blob/master/GUIDE.md');
5357
};
5458
});
59+
60+
function checkIfScriptsExist({ dir }) {
61+
const filePath = path.resolve(dir, 'package.json');
62+
const json = JSON.parse(fs.readFileSync(filePath).toString());
63+
const { test, build } = json.scripts || {};
64+
return {
65+
testExists:
66+
Boolean(test) && test !== 'echo "Error: no test specified" && exit 1',
67+
buildExists: Boolean(build),
68+
};
69+
}

0 commit comments

Comments
 (0)