/
addShipConfig.js
71 lines (66 loc) · 1.9 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
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,
useMonorepo,
mainVersionFile,
packagesToBump,
packagesToPublish,
dir,
dryRun,
}) =>
await runStep({ title: 'Creating ship.config.js' }, async () => {
const { buildExists } = checkIfScriptsExist({ dir });
const config = {
...(isScoped &&
isPublic && {
publishCommand: ({ defaultCommand, tag }) =>
`${defaultCommand} --access public --tag ${tag}`,
}),
...(useMonorepo && {
monorepo: {
mainVersionFile,
packagesToBump,
packagesToPublish,
},
}),
...(!buildExists && { buildCommand: () => null }),
};
const isConfigEmpty = Object.keys(config).length === 0;
if (dryRun) {
print(`ship.config.js`);
print(serialize(config));
} else if (!isConfigEmpty) {
const filePath = path.resolve(dir, 'ship.config.js');
fs.writeFileSync(
filePath,
`module.exports = ${serialize(config, { unsafe: true })};`
);
await runPrettier({ filePath, dir });
}
return () => {
if (isConfigEmpty) {
print(`${info('✔')} No \`ship.config.js\` required.`);
} else {
print(`${info('✔')} Created \`ship.config.js\`.`);
}
print(' You can learn more about the configuration.');
print(
' > https://community.algolia.com/shipjs/guide/useful-config.html'
);
};
});
function checkIfScriptsExist({ dir }) {
const filePath = path.resolve(dir, 'package.json');
const json = JSON.parse(fs.readFileSync(filePath).toString());
const { build } = json.scripts || {};
return {
buildExists: Boolean(build),
};
}