-
Notifications
You must be signed in to change notification settings - Fork 1
/
npm.index.js
74 lines (70 loc) · 3.47 KB
/
npm.index.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
#!/usr/bin/env node
const utils = require('./src/utils');
const deployment = require('./src/deployment')
/**
* Verifying GitHub action inputs
* @param {*} data The data to verify the inputs of
*/
async function verifyInputs(data) {
if(!data.pkgName || data.pkgName === '')
throw new Error('Missing input "pkg_name"');
if(data.npm){
if(!data.npm.token || data.npm.token === '')
throw new Error('Mising input "npm_access_token"');
}
if(data.github){
if(!data.github.token || data.github.token === '')
throw new Error('Mising input "github_access_token"');
if(!data.github.owner || data.github.owner === '')
throw new Error('Mising input "github_owner"');
if(!data.github.repo || data.github.repo === '')
throw new Error('Mising input "github_repo"');
}
}
(async () => {
try {
const cliArguments = process.argv.slice(3)
const isTriggered = (process.argv[1].includes('deploy-pkg') || process.argv[1].includes('\\npm-package-deployer\\lib\\npm.index.js'));
//Verifying the deploy CLI command was executed
if(process.argv[0].includes('node') && isTriggered && process.argv.length >= 3) {
const packageName = process.argv[2];
if(packageName === '--help')
utils.printHelp();
else {
const workingDirectoryIndex = cliArguments.find(arg => arg.includes('--working-directory='));
const workingDirectory = workingDirectoryIndex > -1 ? cliArguments.slice(workingDirectoryIndex).split('=')[1] : '.'
const data = {
pkgName: packageName,
debug: cliArguments.find(arg => arg.includes('--debug')) !== undefined,
prettyPrint: cliArguments.find(arg => arg.includes('--pretty-print')) !== undefined,
dryRun: cliArguments.find(arg => arg.includes('--dry-run')) !== undefined,
workingDirectory: workingDirectory
}
const npmToken = cliArguments.find(arg => arg.includes('--npm-access-token'));
const npmRegistry = cliArguments.find(arg => arg.includes('--npm-registry'));
const npmScope = cliArguments.find(arg => arg.includes('--npm-scope'));
data.npm = npmToken !== undefined ? {
token: npmToken ? npmToken.split('=')[1]: undefined,
registry: npmRegistry ? npmRegistry.split('=')[1]: 'registry.npmjs.org',
scope: npmScope ? npmScope.split('=')[1]: ''
}: undefined;
const githubToken = cliArguments.find(arg => arg.includes('--github-access-token'));
const githubOwner = cliArguments.find(arg => arg.includes('--github-owner'));
const githubRepo = cliArguments.find(arg => arg.includes('--github-repo'));
data.github = githubToken !== undefined ? {
owner: githubOwner ? githubOwner.split('=')[1]: undefined,
repo: githubRepo ? githubRepo.split('=')[1]: undefined,
token: githubToken ? githubToken.split('=')[1]: undefined
}: undefined;
console.log(data)
//Verifying inputs
verifyInputs(data);
await deployment.deploy(data);
}
}
else utils.printHelp()
} catch (error) {
console.log(error);
throw Error(error);
}
})();