Skip to content

Commit 0cef1ac

Browse files
committed
added support for .npmscriptrc.js and custom config build target.
1 parent 895c273 commit 0cef1ac

5 files changed

Lines changed: 149 additions & 93 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ npm-debug.log
44
coverage
55
node_modules
66
test/dist
7+
test/dist-dev

.npmscriptrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,18 @@
1313
"copy": [{ "source": "./test/data2", "destination": "./test/dist"}],
1414
"scripts": ["cp ./test/data/test.json ./test/dist/test.json"],
1515
"chmod": [{ "path": "./test/dist/TestDummy.js", "mode": "755"}]
16+
},
17+
18+
"build-dev":
19+
{
20+
"babel":
21+
{
22+
"source": "test/src",
23+
"destination": "test/dist-dev",
24+
"options": ["--source-maps true"]
25+
},
26+
"copy": [{ "source": "./test/data2", "destination": "./test/dist-dev"}],
27+
"scripts": ["cp ./test/data/test.json ./test/dist-dev/test.json"],
28+
"chmod": [{ "path": "./test/dist-dev/TestDummy.js", "mode": "755"}]
1629
}
1730
}

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ Requirements: Node v5+ / NPM 3+
1212

1313
Provides NPM scripts for building ES6 projects using Babel for all TyphonJS NPM modules and beyond.
1414

15-
This NPM module uses entries defined in the `build.babel` entry located in `.npmscriptrc` in the root path of a project.
15+
This NPM module uses entries defined in the `build.babel` entry located in `.npmscriptrc` or `.npmscriptrc.js` in the
16+
root path of a project. An optional argument may be supplied to change the entry from `build.babel` to `<custom>.babel`.
1617

17-
In addition the following Babel modules are installed that are handy for ES6 NPM modules:
18+
In addition the following Babel modules are installed that are handy for ES6+ development:
19+
- [babel-eslint](https://www.npmjs.com/package/babel-eslint)
1820
- [babel-plugin-add-module-exports](https://www.npmjs.com/package/babel-plugin-add-module-exports)
19-
- [babel-plugin-module-alias](https://www.npmjs.com/package/babel-plugin-module-alias)
21+
- [babel-plugin-module-resolver](https://www.npmjs.com/package/babel-plugin-module-resolver)
22+
- [babel-plugin-transform-runtime](https://www.npmjs.com/package/babel-plugin-transform-runtime)
23+
- [babel-preset-latest](https://www.npmjs.com/package/babel-preset-latest)
24+
- [babel-preset-stage-2](https://www.npmjs.com/package/babel-preset-stage-2)
2025

2126
For the latest significant changes please see the [CHANGELOG](https://github.com/typhonjs-node-npm-scripts/typhonjs-npm-scripts-build-babel/blob/master/CHANGELOG.md).
2227

@@ -28,10 +33,10 @@ To configure the build script provide this entry in `package.json` scripts entry
2833

2934
```
3035
"devDependencies": {
31-
"typhonjs-npm-scripts-build-babel": "^0.5.0"
36+
"typhonjs-npm-scripts-build-babel": "^0.6.0"
3237
},
3338
"scripts": {
34-
"build": "babel-node ./node_modules/typhonjs-npm-scripts-build-babel/scripts/build.js"
39+
"build": "babel-node ./node_modules/typhonjs-npm-scripts-build-babel/scripts/build.js <optional custom build entry>"
3540
},
3641
```
3742

@@ -102,4 +107,4 @@ The basic configuration with optional actions follows:
102107
}
103108
```
104109

105-
Please note that you can add comments to `.npmscriptrc`. Also please note that the build script performs a final sanity check to ensure that there are files / directories in the destination directory otherwise an exception is thrown.
110+
Please note that you can add comments to `.npmscriptrc` and that `.npmscriptrc.js` is required and needs to use `module.exports`. Also please note that the build script performs a final sanity check to ensure that there are files / directories in the destination directory otherwise an exception is thrown.

scripts/build.js

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,63 +39,85 @@ var cp = require('child_process');
3939
var fs = require('fs-extra');
4040
var stripJsonComments = require('strip-json-comments');
4141

42-
// Verify that `.npmscriptrc` exists.
42+
var buildEntry = 'build';
43+
44+
// Potentially set a new testEntry.
45+
if (typeof process.argv[2] === 'string')
46+
{
47+
buildEntry = process.argv[2];
48+
}
49+
50+
// Verify that `Babel` exists.
4351
/* istanbul ignore next */
4452
try
4553
{
46-
if (!fs.statSync('./.npmscriptrc').isFile())
54+
if (!fs.statSync('./node_modules/.bin/babel').isFile())
4755
{
48-
throw new Error("'.npmscriptrc' not found in root path.");
56+
throw new Error("could not locate Babel at './node_modules/.bin/babel'.");
4957
}
5058
}
5159
catch(err)
5260
{
5361
throw new Error("typhonjs-npm-scripts-build-babel error: " + err);
5462
}
5563

56-
// Verify that `Babel` exists.
64+
var configInfo;
65+
66+
// Attempt to require `.npmscriptrc.js`
5767
/* istanbul ignore next */
5868
try
5969
{
60-
if (!fs.statSync('./node_modules/.bin/babel').isFile())
70+
if (fs.statSync('./.npmscriptrc.js').isFile())
6171
{
62-
throw new Error("could not locate Babel at './node_modules/.bin/babel'.");
72+
configInfo = require('./.npmscriptrc.js');
6373
}
6474
}
65-
catch(err)
75+
catch (err) { /* nop */ }
76+
77+
// Attempt to load `.npmscriptrc` and strip comments.
78+
/* istanbul ignore next */
79+
try
6680
{
67-
throw new Error("typhonjs-npm-scripts-build-babel error: " + err);
81+
if (fs.statSync('./.npmscriptrc').isFile())
82+
{
83+
configInfo = JSON.parse(stripJsonComments(fs.readFileSync('./.npmscriptrc', 'utf-8')));
84+
}
6885
}
86+
catch (err) { /* nop */ }
6987

70-
// Load `.npmscriptrc` and strip comments.
71-
var configInfo = JSON.parse(stripJsonComments(fs.readFileSync('./.npmscriptrc', 'utf-8')));
88+
// Exit now if no configInfo object has been loaded.
89+
if (typeof configInfo !== 'object')
90+
{
91+
throw new Error("TyphonJS NPM script (build-babel) could not load `./npmscriptrc.js` or `./npmscriptrc`.");
92+
}
7293

7394
// Verify that `build` entry is an object.
7495
/* istanbul ignore if */
75-
if (typeof configInfo.build !== 'object')
96+
if (typeof configInfo[buildEntry] !== 'object')
7697
{
7798
throw new Error(
78-
"typhonjs-npm-scripts-build-babel error: 'build' entry is not an object or is missing in '.npmscriptrc'.");
99+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry +
100+
"' entry is not an object or is missing in config file.");
79101
}
80102

81-
82103
// Verify that `build.babel` entry is an object.
83104
/* istanbul ignore if */
84-
if (typeof configInfo.build.babel !== 'object')
105+
if (typeof configInfo[buildEntry].babel !== 'object')
85106
{
86107
throw new Error(
87-
"typhonjs-npm-scripts-build-babel error: 'build.babel' entry is not an object or is missing in '.npmscriptrc'.");
108+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry +
109+
".babel' entry is not an object or is missing in config file.");
88110
}
89111

90-
var babelConfig = configInfo.build.babel;
112+
var babelConfig = configInfo[buildEntry].babel;
91113

92114
// Verify that source entry is a string.
93115
/* istanbul ignore if */
94116
if (typeof babelConfig.source !== 'string')
95117
{
96118
throw new Error(
97-
"typhonjs-npm-scripts-build-babel error: 'build.babel.source' entry is not a string or is missing in "
98-
+ "'.npmscriptrc'.");
119+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry + ".babel.source' entry is not a string or is missing in "
120+
+ "config file.");
99121
}
100122

101123
// Verify that destination entry is a string.
@@ -104,7 +126,7 @@ if (typeof babelConfig.destination !== 'string')
104126
{
105127
throw new Error(
106128
"typhonjs-npm-scripts-build-babel error: 'build.babel.destination' entry is not a string or is missing in "
107-
+ "'.npmscriptrc'.");
129+
+ "config file.");
108130
}
109131

110132
// Verify that source entry is a directory.
@@ -113,7 +135,7 @@ try
113135
{
114136
if (!fs.statSync(babelConfig.source).isDirectory())
115137
{
116-
throw new Error("'build.babel.source' entry is not a directory: " + babelConfig.source);
138+
throw new Error("'" + buildEntry + ".babel.source' entry is not a directory: " + babelConfig.source);
117139
}
118140
}
119141
catch(err)
@@ -130,7 +152,7 @@ try
130152
{
131153
if (!fs.statSync(babelConfig.destination).isDirectory())
132154
{
133-
throw new Error("'build.babel.destination' entry is not a directory: " + babelConfig.destination);
155+
throw new Error("'" + buildEntry + "babel.destination' entry is not a directory: " + babelConfig.destination);
134156
}
135157
}
136158
catch(err)
@@ -148,7 +170,8 @@ if (typeof babelConfig.options !== 'undefined')
148170
if (!Array.isArray(babelConfig.options))
149171
{
150172
throw new Error(
151-
"typhonjs-npm-scripts-build-babel error: 'build.babel.options' entry is not an `array` in '.npmscriptrc'.");
173+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry +
174+
".babel.options' entry is not an `array` in '.npmscriptrc'.");
152175
}
153176

154177
exec += ' ' + babelConfig.options.join(' ');
@@ -164,26 +187,26 @@ var cntr;
164187
// Copy files -------------------------------------------------------------------------------------------------------
165188

166189
// Potentially copy files if a copy array is present
167-
if (Array.isArray(configInfo.build.copy))
190+
if (Array.isArray(configInfo[buildEntry].copy))
168191
{
169-
for (cntr = 0; cntr < configInfo.build.copy.length; cntr++)
192+
for (cntr = 0; cntr < configInfo[buildEntry].copy.length; cntr++)
170193
{
171-
var copyEntry = configInfo.build.copy[cntr];
194+
var copyEntry = configInfo[buildEntry].copy[cntr];
172195

173196
/* istanbul ignore if */
174197
if (typeof copyEntry !== 'object')
175198
{
176199
throw new Error(
177-
"typhonjs-npm-scripts-build-babel error: 'build.copy' entry `" +cntr
178-
+"` is not an `object` in '.npmscriptrc'.");
200+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry + ".copy' entry `" +cntr
201+
+"` is not an `object` in config file.");
179202
}
180203

181204
/* istanbul ignore if */
182205
if (typeof copyEntry.source !== 'string' || typeof copyEntry.destination !== 'string')
183206
{
184207
throw new Error(
185-
"typhonjs-npm-scripts-build-babel error: 'build.copy' entry `" +cntr
186-
+ "` has improperly formatted `source` or `destination` fields in '.npmscriptrc'.");
208+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry + ".copy' entry `" +cntr
209+
+ "` has improperly formatted `source` or `destination` fields in config file.");
187210
}
188211

189212
console.log('typhonjs-npm-scripts-build-babel copy: ' + copyEntry.source + ' ' + copyEntry.destination);
@@ -195,36 +218,36 @@ if (Array.isArray(configInfo.build.copy))
195218
// Commands / scripts -----------------------------------------------------------------------------------------------
196219

197220
// Potentially run commands or scripts if a scripts array is present
198-
if (Array.isArray(configInfo.build.scripts))
221+
if (Array.isArray(configInfo[buildEntry].scripts))
199222
{
200223
var runner = require('typhonjs-npm-scripts-runner');
201224

202-
runner.run('.npmscriptrc', 'build.scripts', 'typhonjs-npm-scripts-build-babel');
225+
runner.run(['.npmscriptrc', '.npmscriptrc.js'], buildEntry + '.scripts', 'typhonjs-npm-scripts-build-babel');
203226
}
204227

205228
// Chmod files ------------------------------------------------------------------------------------------------------
206229

207230
// Potentially chmod files if a chmod array is present
208-
if (Array.isArray(configInfo.build.chmod))
231+
if (Array.isArray(configInfo[buildEntry].chmod))
209232
{
210-
for (cntr = 0; cntr < configInfo.build.chmod.length; cntr++)
233+
for (cntr = 0; cntr < configInfo[buildEntry].chmod.length; cntr++)
211234
{
212-
var chmodEntry = configInfo.build.chmod[cntr];
235+
var chmodEntry = configInfo[buildEntry].chmod[cntr];
213236

214237
/* istanbul ignore if */
215238
if (typeof chmodEntry !== 'object')
216239
{
217240
throw new Error(
218-
"typhonjs-npm-scripts-build-babel error: 'build.cntr' chmod `" +cntr
219-
+"` is not an `object` in '.npmscriptrc'.");
241+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry + ".chmod' `" +cntr
242+
+"` is not an `object` in config file.");
220243
}
221244

222245
/* istanbul ignore if */
223246
if (typeof chmodEntry.path !== 'string' || typeof chmodEntry.mode !== 'string')
224247
{
225248
throw new Error(
226-
"typhonjs-npm-scripts-build-babel error: 'build.chmod' entry `" +cntr
227-
+ "` has improperly formatted `path` or `mode` fields in '.npmscriptrc'.");
249+
"typhonjs-npm-scripts-build-babel error: '" + buildEntry + ".chmod' entry `" +cntr
250+
+ "` has improperly formatted `path` or `mode` fields in config file.");
228251
}
229252

230253
console.log('typhonjs-npm-scripts-build-babel chmod: ' + chmodEntry.path + ' ' + chmodEntry.mode);

0 commit comments

Comments
 (0)