Skip to content

Commit

Permalink
Update eject script
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbruegge committed Oct 8, 2018
1 parent cb025de commit 925d9fa
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 67 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@
"html-webpack-harddisk-plugin": "^0.2.0",
"html-webpack-plugin": "^3.2.0",
"husky": "^1.1.1",
"inquirer": "^6.2.0",
"istanbul-instrumenter-loader-fix": "^3.0.2",
"lint-staged": "^7.3.0",
"mkdirp": "^0.5.1",
"mocha": "^5.2.0",
"mocha-webpack": "^2.0.0-beta.0",
"node-sass": "^4.9.3",
Expand Down
143 changes: 94 additions & 49 deletions scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,99 @@
const fs = require('fs-extra');
const path = require('path');
const mkdirp = require('mkdirp');
const spawn = require('cross-spawn');
const inquirer = require('inquirer');

const ownPackageJsonPath = path.resolve(__dirname, '..', 'package.json');
const appPackageJsonPath = path.join(process.cwd(), 'package.json');
const ownPackageJson = JSON.parse(fs.readFileSync(ownPackageJsonPath));
const appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath));
const scriptsPath = path.join(process.cwd(), '.scripts');

const mochaArgs = '{' + appPackageJson['mocha-webpack'].include.join(',') + '}';

// Declaring new scripts
const scripts = {
start:
'cross-env NODE_ENV=development webpack-dev-server --config configs/webpack.config.js',
test:
'cross-env NODE_ENV=test nyc mocha-webpack --timeout=100000 --colors --webpack-config configs/webpack.config.test.js ' +
mochaArgs,
build:
'cross-env NODE_ENV=production webpack --config configs/webpack.config.js',
clean: 'rimraf build .tmp .nyc_output coverage'
};

// Declare the new dependencies, excluding self
let devDependencies = {};
Object.keys(appPackageJson.devDependencies)
.filter(dep => dep !== ownPackageJson.name)
.forEach(dep => {
devDependencies[dep] = appPackageJson.devDependencies[dep];
const appPath = (...paths) => path.join(process.cwd(), ...paths);
const usingPnpm = fs.existsSync(appPath('shrinkwrap.yaml'));

const ownDependencyNames = [
'inquirer',
'cross-spawn',
'chalk',
'fs-extra',
'mkdirp'
];

inquirer
.prompt([
{
type: 'confirm',
name: 'confirm',
default: false,
message:
'Are you sure you want to eject? This flavor is designed so that you do not have to do so, if I forgot a use case, please open an issue at\n\nhttps://github.com/cyclejs-community/one-fits-all\n\nThis process is NOT reversable, it will remove all traces of the flavor from your project\n' +
(!usingPnpm
? ''
: '\nYou are using pnpm which will cause this script to delete your shrinkwrap.yaml and node_modules and reinstall afterwards\n')
}
])
.then(answers => {
if (!answers.confirm) {
return;
}

const ownPackageJsonPath = path.resolve(
__dirname,
'..',
'package.json'
);
const appPackageJsonPath = appPath('package.json');
const ownPackageJson = JSON.parse(fs.readFileSync(ownPackageJsonPath));
const appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath));

const mochaArgs = appPackageJson['mocha-webpack'].include.join(' ');

// Declaring new scripts
const scripts = {
start:
'cross-env NODE_ENV=development webpack-dev-server --config configs/webpack.config.js',
test:
'cross-env NODE_ENV=test nyc mocha-webpack --colors --webpack-config configs/webpack.config.js ' +
mochaArgs,
build:
'cross-env NODE_ENV=production webpack --config configs/webpack.config.js',
clean: 'rimraf build .nyc_output coverage'
};

// Declare the new dependencies, excluding self
let devDependencies = {};
Object.keys(appPackageJson.devDependencies)
.filter(dep => dep !== ownPackageJson.name)
.forEach(dep => {
devDependencies[dep] = appPackageJson.devDependencies[dep];
});
devDependencies = Object.assign(
{},
devDependencies,
Object.keys(ownPackageJson.dependencies)
.filter(dep => ownDependencyNames.indexOf(dep) === -1)
.reduce(
(a, c) => ({ ...a, [c]: ownPackageJson.dependencies[c] }),
{}
)
);

// Write the new package.json
const newPackageJson = Object.assign({}, appPackageJson, {
scripts: scripts,
devDependencies: devDependencies
});
delete newPackageJson['mocha-webpack'];
fs.writeFileSync(
appPackageJsonPath,
JSON.stringify(newPackageJson, null, 2)
);

fs.mkdirSync(appPath('configs'));
fs.copySync(
path.join(__dirname, '..', 'configs', 'webpack.config.js'),
appPath('configs', 'webpack.config.js')
);

if (usingPnpm) {
fs.removeSync(appPath('shrinkwrap.yaml'));
fs.removeSync(appPath('node_modules'));
spawn.sync('pnpm', ['install'], { stdio: 'inherit' });
}
});
devDependencies = Object.assign(
{},
devDependencies,
ownPackageJson.dependencies
);

// Write the new package.json
const newPackageJson = Object.assign({}, appPackageJson, {
scripts: scripts,
devDependencies: devDependencies
});
delete newPackageJson['mocha-webpack'];
fs.writeFileSync(appPackageJsonPath, JSON.stringify(newPackageJson, null, 2));

fs.mkdirSync(path.join(process.cwd(), 'configs'));
fs.copySync(
path.join(__dirname, '..', 'configs', 'webpack.config.js'),
'configs/webpack.config.js'
);
fs.copySync(
path.join(__dirname, '..', 'configs', 'webpack.config.test.js'),
'configs/webpack.config.test.js'
);
2 changes: 2 additions & 0 deletions scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ module.exports = function init(appPath, appName, verboseOpts) {
...devDependencies
};

appPackage['one-fits-all'] = true;

if (cli === 'yarn') {
appPackage.resolutions = {
'**/typescript': '2.8'
Expand Down
18 changes: 4 additions & 14 deletions shrinkwrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ dependencies:
html-webpack-harddisk-plugin: 0.2.0
html-webpack-plugin: 3.2.0
husky: 1.1.1
inquirer: 6.2.0
istanbul-instrumenter-loader-fix: 3.0.2
lint-staged: 7.3.0
mkdirp: 0.5.1
mocha: 5.2.0
mocha-webpack: 2.0.0-beta.0
node-sass: 4.9.3
Expand Down Expand Up @@ -654,7 +656,6 @@ packages:
resolution:
integrity: sha1-06ioOzGapneTZisT52HHkRQiMG4=
/ansi-escapes/3.1.0:
dev: true
engines:
node: '>=4'
resolution:
Expand Down Expand Up @@ -1755,7 +1756,6 @@ packages:
resolution:
integrity: sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==
/chardet/0.7.0:
dev: true
resolution:
integrity: sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
/chokidar/2.0.4:
Expand Down Expand Up @@ -1848,7 +1848,6 @@ packages:
/cli-cursor/2.1.0:
dependencies:
restore-cursor: 2.0.0
dev: true
engines:
node: '>=4'
resolution:
Expand All @@ -1869,7 +1868,6 @@ packages:
resolution:
integrity: sha1-nxXPuwcFAFNpIWxiasfQWrkN1XQ=
/cli-width/2.2.0:
dev: true
resolution:
integrity: sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=
/cliui/3.2.0:
Expand Down Expand Up @@ -3284,7 +3282,6 @@ packages:
chardet: 0.7.0
iconv-lite: 0.4.24
tmp: 0.0.33
dev: true
engines:
node: '>=4'
resolution:
Expand Down Expand Up @@ -3385,7 +3382,6 @@ packages:
/figures/2.0.0:
dependencies:
escape-string-regexp: 1.0.5
dev: true
engines:
node: '>=4'
resolution:
Expand Down Expand Up @@ -4214,7 +4210,6 @@ packages:
/iconv-lite/0.4.24:
dependencies:
safer-buffer: 2.1.2
dev: true
engines:
node: '>=0.10.0'
resolution:
Expand Down Expand Up @@ -4334,7 +4329,6 @@ packages:
string-width: 2.1.1
strip-ansi: 4.0.0
through: 2.3.8
dev: true
engines:
node: '>=6.0.0'
resolution:
Expand Down Expand Up @@ -5661,7 +5655,6 @@ packages:
resolution:
integrity: sha512-ji6J5enbMyGRHIAkAOu3WdV8nggqviKCEKtXcOqfphZZtQrmHKycfynJ2V7eVPUA4NhJ6V7Wf4TmGbTwKE9B6g==
/mute-stream/0.0.7:
dev: true
resolution:
integrity: sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=
/nan/2.11.1:
Expand Down Expand Up @@ -6040,7 +6033,6 @@ packages:
/onetime/2.0.1:
dependencies:
mimic-fn: 1.2.0
dev: true
engines:
node: '>=4'
resolution:
Expand Down Expand Up @@ -7320,7 +7312,6 @@ packages:
dependencies:
onetime: 2.0.1
signal-exit: 3.0.2
dev: true
engines:
node: '>=4'
resolution:
Expand Down Expand Up @@ -7351,7 +7342,6 @@ packages:
/run-async/2.3.0:
dependencies:
is-promise: 2.1.0
dev: true
engines:
node: '>=0.12.0'
resolution:
Expand Down Expand Up @@ -8117,7 +8107,6 @@ packages:
resolution:
integrity: sha512-mVzjRxuWnDKs/qH1rbOJEVHLlSX9kty9lpi7lMvLgU9S74mQ8/Ozg9UPcKxShh0qG2NZ+NyPOPpcZU4C1Eld9A==
/through/2.3.8:
dev: true
resolution:
integrity: sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=
tarball: 'http://registry.npmjs.org/through/-/through-2.3.8.tgz'
Expand Down Expand Up @@ -8155,7 +8144,6 @@ packages:
/tmp/0.0.33:
dependencies:
os-tmpdir: 1.0.2
dev: true
engines:
node: '>=0.6.0'
resolution:
Expand Down Expand Up @@ -9035,8 +9023,10 @@ specifiers:
html-webpack-harddisk-plugin: ^0.2.0
html-webpack-plugin: ^3.2.0
husky: ^1.1.1
inquirer: ^6.2.0
istanbul-instrumenter-loader-fix: ^3.0.2
lint-staged: ^7.3.0
mkdirp: ^0.5.1
mocha: ^5.2.0
mocha-webpack: ^2.0.0-beta.0
node-sass: ^4.9.3
Expand Down
5 changes: 1 addition & 4 deletions template/pnpmfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ module.exports = {
};

function readPackage(pkg, context) {
if (
pkg.devDependencies &&
pkg.devDependencies['cycle-scripts-one-fits-all']
) {
if (pkg['one-fits-all']) {
pkg.devDependencies = {
...pkg.devDependencies,
tslint: '*',
Expand Down

0 comments on commit 925d9fa

Please sign in to comment.