Skip to content

Commit 72dd928

Browse files
authored
Converted package.sh and zip.sh to Javascript files. (#219)
1 parent e62a295 commit 72dd928

File tree

6 files changed

+118
-51
lines changed

6 files changed

+118
-51
lines changed

npm/package.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var shell = require('shelljs'),
2+
path = require('path'),
3+
async = require('async'),
4+
fs = require('fs'),
5+
exists,
6+
codegen,
7+
codegens,
8+
codegen_path,
9+
getSubfolders,
10+
individual_test,
11+
commandOut;
12+
13+
const args = process.argv[2],
14+
PATH_TO_CODEGENS_FOLDER = path.resolve(__dirname, '../codegens');
15+
16+
// throw JS error when any shell.js command encounters an error
17+
shell.config.fatal = true;
18+
19+
// ensure that the working tree is clean before packaging
20+
commandOut = shell.exec('source ./npm/package/require_clean_work_tree.sh');
21+
if (commandOut.code !== 0) {
22+
shell.exit(1);
23+
}
24+
25+
getSubfolders = (folder) => {
26+
return fs.readdirSync(folder)
27+
.map((subfolder) => { return { path: path.join(folder, subfolder), name: subfolder}; })
28+
.filter((obj) => { return fs.statSync(obj.path).isDirectory(); });
29+
};
30+
31+
individual_test = (codegen) => {
32+
33+
console.log(`Creating package for ${codegen}`);
34+
async.series([
35+
function (next) {
36+
console.log(`Running codegen test for codegen ${codegen}`);
37+
commandOut = shell.exec(`npm run test ${codegen} --color always`);
38+
if (commandOut.code !== 0) {
39+
console.error(`Failed to run codegen test on codegen ${codegen}, here's the error:`);
40+
return next(commandOut.stderr);
41+
}
42+
console.log(commandOut.stdout);
43+
return next();
44+
},
45+
function (next) {
46+
console.log(`Generating zip for codegen ${codegen}`);
47+
commandOut = shell.exec(`npm run zip ${codegen} --color always`);
48+
if (commandOut.code !== 0) {
49+
console.error(`Failed to zip codegen ${codegen}, here's the error:`);
50+
return next(commandOut.stderr);
51+
}
52+
console.log(commandOut.stdout);
53+
}
54+
], (err) => {
55+
console.error(err);
56+
shell.exit(1);
57+
});
58+
};
59+
60+
if (args) {
61+
codegen = args;
62+
codegen_path = path.join(PATH_TO_CODEGENS_FOLDER, codegen);
63+
try {
64+
exists = fs.statSync(codegen_path).isDirectory();
65+
}
66+
catch (err) {
67+
console.log(`Codegen ${codegen} doesn't exist, please enter a valid name`);
68+
console.log(err);
69+
shell.exit(1);
70+
}
71+
if (exists) {
72+
individual_test(codegen);
73+
}
74+
}
75+
else {
76+
console.log('Packaging all the codegens');
77+
codegens = getSubfolders(PATH_TO_CODEGENS_FOLDER);
78+
codegens.forEach((codegen) => {
79+
individual_test(codegen.name);
80+
});
81+
}

npm/package.sh

-36
This file was deleted.

npm/zip.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var shell = require('shelljs'),
2+
path = require('path'),
3+
fs = require('fs'),
4+
codegen_path,
5+
commandOut,
6+
exists;
7+
8+
const codegen = process.argv[2],
9+
PATH_TO_CODEGENS_FOLDER = path.resolve(__dirname, '../codegens');
10+
11+
// throw JS error when any shell.js command encounters an error
12+
shell.config.fatal = true;
13+
14+
codegen_path = path.join(PATH_TO_CODEGENS_FOLDER, codegen);
15+
16+
try {
17+
exists = fs.statSync(codegen_path).isDirectory();
18+
}
19+
catch (err) {
20+
console.log(`Codegen ${codegen} doesn't exist, please enter a valid name`);
21+
shell.exit(1);
22+
}
23+
if (exists) {
24+
console.log(`${codegen} : zip`);
25+
shell.pushd(codegen_path, 'q');
26+
commandOut = shell.exec('npm pack --color always');
27+
if (commandOut.code !== 0) {
28+
console.error(`Failed to run pre-package.js for codegen ${codegen}, here's the error:`);
29+
console.log(commandOut.stderr);
30+
shell.exit(1);
31+
}
32+
shell.popd(null, 'q');
33+
}

npm/zip.sh

-12
This file was deleted.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
},
99
"scripts": {
1010
"test": "npm/test.sh",
11-
"package": "npm/package.sh",
12-
"zip": "npm/zip.sh",
11+
"package": "node npm/package.js",
12+
"zip": "node npm/zip.js",
1313
"prepack": "node npm/pre-package.js",
1414
"postinstall": "node npm/deepinstall.js",
1515
"deepinstall": "node npm/deepinstall.js",

test/codegen/structure.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const expectedOptions = {
4141
name: 'Set request timeout',
4242
type: 'positiveInteger',
4343
default: 0,
44-
description: 'Set number of milliseconds the request should wait for a response before timing out (use 0 for infinity)'
44+
description: 'Set number of milliseconds the request should wait' +
45+
' for a response before timing out (use 0 for infinity)'
4546
},
4647
followRedirect: {
4748
name: 'Follow redirects',

0 commit comments

Comments
 (0)