-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-install.js
84 lines (66 loc) · 3.25 KB
/
post-install.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
78
79
80
81
82
83
84
const jsonfile = require('jsonfile');
const write = require('write');
const { defaultLocalConfig, stringify, log } = require('@usearcade/arcade-libs');
const fs = require('fs').promises;
// This is needed because of a bug with local testing
// Solution + more discussion here: https://github.com/npm/npm/issues/16990#issuecomment-349731142
const projectDir = process.env.INIT_CWD || path.resolve("../../", __dirname);
// Arcade will have to be the package name
// File that handles running scripts
const executableFile = `const Arcade = require('@usearcade/arcade-js');
const config = require('../arcade-config.json');
if (process.argv[2] === 'update=true') {
Arcade.updateTokens(config, {});
} else {
Arcade.buildTokens(config, {});
}
`;
// This file gets triggered after install
(async () => {
try {
// Write test.js file with our operations
log({ msg: 'Executing arcade-js postinstall...' });
// Make sure config is there, generate one if not
await handleConfig();
// Write our executable scripts
write(`${projectDir}/.arcade/execute.js`, executableFile, { newline: true, overwrite: true });
// Add script to scripts part of package that will specify actions
await addScriptToPackage({ key: "arcade-update", value: "node ./.arcade/execute.js update=true", force: true });
await addScriptToPackage({ key: "arcade-build", value: "node ./.arcade/execute.js", force: true });
log({ msg: '\nInstall success.', type: 'success' });
log({ msg: 'Ensure arcade-config.json contains your project details.' });
log({ msg: '\n npm run arcade-build', type: 'command' });
log({ msg: ' to build your tokens in their current configuration.' });
log({ msg: '\n npm run arcade-update', type: 'command' });
log({ msg: ' to update your tokens and rebuild them.' });
log({ msg: '\n\n' });
} catch (err) {
process.exitCode = 1;
log({ msg: err, type: 'error' });
}
})();
// Quick way to modify scripts section of the package we're installing to
async function addScriptToPackage ({ key, value, force }) {
const file = `${projectDir}/package.json`;
const package = await jsonfile.readFile(file);
if (!package.scripts) { package.scripts = {} }
if (package.scripts[key] && !force) { return Promise.reject(`Script with key ${key} already exists. Specify force=true to overwrite.`) }
package.scripts[key] = value;
await jsonfile.writeFile(`${projectDir}/package.json`, package);
return package;
}
// Make sure config is there, generate one if not
async function handleConfig () {
// See if config exists...
try {
const file = `${projectDir}/arcade-config.json`;
const config = await jsonfile.readFile(file);
if (config) { return config }
// If not, reading the file will fail and will be sent down here...
} catch (err) {
const config = defaultLocalConfig();
config.access_token = "";
await fs.writeFile(`${projectDir}/arcade-config.json`, stringify(config));
return config;
}
}