-
Notifications
You must be signed in to change notification settings - Fork 30
/
addShipConfig.js
77 lines (74 loc) · 2.12 KB
/
addShipConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fs from 'fs';
import path from 'path';
import serialize from 'serialize-javascript';
import runStep from '../runStep';
import { runPrettier } from '../../helper';
import { info } from '../../color';
import { print } from '../../util';
export default async ({
isScoped,
isPublic,
baseBranch,
releaseBranch,
useMonorepo,
mainVersionFile,
packagesToBump,
packagesToPublish,
dir,
dryRun,
}) =>
await runStep({ title: 'Creating ship.config.js' }, async () => {
const { testExists, buildExists } = checkIfScriptsExist({ dir });
const config = {
...(isScoped &&
isPublic && {
publishCommand: ({ defaultCommand }) =>
`${defaultCommand} --access public`,
}),
mergeStrategy:
baseBranch === releaseBranch
? {
toSameBranch: [baseBranch],
}
: {
toReleaseBranch: {
[baseBranch]: releaseBranch,
},
},
...(useMonorepo && {
monorepo: {
mainVersionFile,
packagesToBump,
packagesToPublish,
},
}),
...(!testExists && { testCommandBeforeRelease: () => null }),
...(!buildExists && { buildCommand: () => null }),
};
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, { unsafe: true })};`
);
await runPrettier({ filePath, dir });
}
return () => {
print(`${info('✔')} Created \`ship.config.js\`.`);
print(' You can learn more about the configuration.');
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),
};
}