UNPKG

1.23 kBJavaScriptView Raw
1/* eslint-disable */
2// adapted based on rackt/history (MIT)
3// Node 4+
4const execSync = require('child_process').execSync;
5const fs = require('fs');
6const path = require('path');
7const json = require('mrm-core').json;
8
9const defaultsName = 'defaults';
10const distDirectory = 'dist';
11const localDir = process.cwd().split('node_modules').shift();
12
13writeScript();
14writeDist();
15
16// Create webpack-defaults in package.json if it doesn’t exist
17function writeScript() {
18 const pkg = json(path.resolve(localDir, 'package.json'));
19 const scriptPath = ['scripts', defaultsName];
20 if (!pkg.get(scriptPath)) {
21 pkg
22 .set(scriptPath, 'webpack-defaults')
23 .save();
24 }
25}
26
27// Compile sources in case of installation from Git
28function writeDist() {
29 fs.stat(distDirectory, function(error, stat) {
30 // Skip building on Travis
31 if (process.env.TRAVIS) {
32 return;
33 }
34
35 if (error || !stat.isDirectory()) {
36 // Create a directory to avoid getting stuck
37 // in postinstall loop
38 fs.mkdirSync(distDirectory);
39 exec('npm install --only=dev');
40 exec('npm run build');
41 }
42 });
43}
44
45function exec(command) {
46 execSync(command, {
47 // Print stdin/stdout/stderr
48 stdio: 'inherit'
49 });
50}