Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 26, 2019
1 parent 38c3180 commit 1ba26be
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 79 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
before_install:
- npm i -g npm
- npm i -g yarn
86 changes: 37 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const fs = require('fs');
const path = require('path');
const minimist = require('minimist');
const arrify = require('arrify');
Expand Down Expand Up @@ -30,7 +29,7 @@ const CONFIG_FILES = [
'.jscs.yaml'
];

function buildTestScript(test) {
const buildTestScript = test => {
if (test && test !== DEFAULT_TEST_SCRIPT) {
// Don't add if it's already there
if (!/^xo( |$)/.test(test)) {
Expand All @@ -41,36 +40,32 @@ function buildTestScript(test) {
}

return 'xo';
}
};

function warnConfigFile(pkgCwd) {
const files = CONFIG_FILES.filter(file => pathExists.sync(path.join(pkgCwd, file)));
const warnConfigFile = packageCwd => {
const files = CONFIG_FILES.filter(file => pathExists.sync(path.join(packageCwd, file)));

if (files.length === 0) {
return;
}

console.log(`${files.join(' & ')} can probably be deleted now that you're using XO.`);
}
};

module.exports = (opts = {}) => {
const ret = readPkgUp.sync({
cwd: opts.cwd,
module.exports = async (options = {}) => {
const packageResult = readPkgUp.sync({
cwd: options.cwd,
normalize: false
});
const pkg = ret.pkg || {};
const pkgPath = ret.path || path.resolve(opts.cwd || '', 'package.json');
const pkgCwd = path.dirname(pkgPath);

pkg.scripts = pkg.scripts || {};
pkg.scripts.test = buildTestScript(pkg.scripts.test);
}) || {};
const packageJson = packageResult.package || {};
const packagePath = packageResult.path || path.resolve(options.cwd || '', 'package.json');
const packageCwd = path.dirname(packagePath);

const cli = minimist(opts.args || argv());
const {unicorn} = cli;
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.test = buildTestScript(packageJson.scripts.test);

const cli = minimist(options.args || argv());
delete cli._;
delete cli.unicorn;
delete cli.init;

for (const option of PLURAL_OPTIONS) {
if (cli[option]) {
Expand All @@ -80,43 +75,36 @@ module.exports = (opts = {}) => {
}

if (Object.keys(cli).length > 0) {
pkg.xo = Object.assign({}, pkg.xo, cli);
packageJson.xo = {...packageJson.xo, ...cli};
}

writePkg.sync(pkgPath, pkg);
writePkg.sync(packagePath, packageJson);

const post = () => {
warnConfigFile(pkgCwd);

// For personal use
if (unicorn) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
pkg.devDependencies.xo = '*';
writePkg.sync(pkgPath, pkg);

for (const file of CONFIG_FILES) {
try {
fs.unlinkSync(path.join(pkgCwd, file));
} catch (_) {}
}
}
warnConfigFile(packageCwd);
};

if (opts.skipInstall) {
return Promise.resolve(post);
if (options.skipInstall) {
post();
return;
}

if (hasYarn(pkgCwd)) {
return execa('yarn', ['add', '--dev', '--ignore-workspace-root-check', 'xo'], {cwd: pkgCwd})
.then(post)
.catch(error => {
if (error.code === 'ENOENT') {
console.error('This project uses Yarn but you don\'t seem to have Yarn installed.\nRun \'npm install --global yarn\' to install it.');
return;
}
throw error;
});
if (hasYarn(packageCwd)) {
try {
await execa('yarn', ['add', '--dev', '--ignore-workspace-root-check', 'xo'], {cwd: packageCwd});
post();
} catch (error) {
if (error.code === 'ENOENT') {
console.error('This project uses Yarn but you don\'t seem to have Yarn installed.\nRun `npm install --global yarn` to install it.');
return;
}

throw error;
}

return;
}

return execa('npm', ['install', '--save-dev', 'xo'], {cwd: pkgCwd}).then(post);
await execa('npm', ['install', '--save-dev', 'xo'], {cwd: packageCwd});
post();
};
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -30,19 +30,19 @@
"xo"
],
"dependencies": {
"arrify": "^1.0.0",
"arrify": "^2.0.1",
"execa": "^1.0.0",
"has-yarn": "^1.0.0",
"has-yarn": "^2.1.0",
"minimist": "^1.1.3",
"path-exists": "^3.0.0",
"read-pkg-up": "^4.0.0",
"path-exists": "^4.0.0",
"read-pkg-up": "^6.0.0",
"the-argv": "^1.0.0",
"write-pkg": "^3.1.0"
"write-pkg": "^4.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"dot-prop": "^4.0.0",
"temp-write": "^3.3.0",
"xo": "^0.23.0"
"ava": "^2.4.0",
"dot-prop": "^5.1.0",
"temp-write": "^4.0.0",
"xo": "^0.25.3"
}
}
11 changes: 4 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ const xoInit = require('xo-init');

## API

### xoInit([options])
### xoInit(options?)

Returns a `Promise`.

#### options

Type: `object`

#### cwd

Type: `string`<br>
Expand All @@ -39,7 +41,7 @@ Current working directory.

#### args

Type: `Array`<br>
Type: `string[]`<br>
Default: CLI arguments *(`process.argv.slice(2)`)*

Options to put in [XO's config](https://www.npmjs.com/package/xo#config) in `package.json`.
Expand All @@ -62,8 +64,3 @@ For instance, with the arguments `['--space', '--env=node']` the following will
## CLI

Install XO globally `$ npm install --global xo` and run `$ xo --init [<options>]`.


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
17 changes: 8 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import tempWrite from 'temp-write';
import dotProp from 'dot-prop';
import test from 'ava';
import m from '.';
import xoInit from '.';

// Remove --color flag passed on from AVA
// TODO: Remove this when fixed in AVA
Expand All @@ -15,7 +15,7 @@ const {get} = dotProp;
async function run(pkg) {
const filepath = tempWrite.sync(JSON.stringify(pkg), 'package.json');

await m({
await xoInit({
cwd: path.dirname(filepath),
skipInstall: true
});
Expand Down Expand Up @@ -74,7 +74,7 @@ test('has test', async t => {
});

test('has cli args', async t => {
process.argv = originalArgv.concat(['--init', '--space']);
process.argv = originalArgv.concat(['--space']);

const pkg = await run({
scripts: {
Expand All @@ -88,7 +88,7 @@ test('has cli args', async t => {
});

test('has cli args and test', async t => {
process.argv = originalArgv.concat(['--init', '--env=node', '--env=browser']);
process.argv = originalArgv.concat(['--env=node', '--env=browser']);

const pkg = await run({
scripts: {
Expand All @@ -103,7 +103,7 @@ test('has cli args and test', async t => {
});

test('has cli args and existing config', async t => {
process.argv = originalArgv.concat(['--init', '--space']);
process.argv = originalArgv.concat(['--space']);

const pkg = await run({
xo: {
Expand All @@ -118,7 +118,7 @@ test('has cli args and existing config', async t => {
});

test('has existing config without cli args', async t => {
process.argv = originalArgv.concat(['--init']);
process.argv = originalArgv;

const pkg = await run({
xo: {
Expand All @@ -133,7 +133,6 @@ test('has existing config without cli args', async t => {

test('has everything covered when it comes to config', async t => {
process.argv = originalArgv.concat([
'--init',
'--space',
'--esnext',
'--no-semicolon',
Expand Down Expand Up @@ -162,12 +161,12 @@ test('has everything covered when it comes to config', async t => {

test('installs the XO dependency', async t => {
const filepath = tempWrite.sync(JSON.stringify({}), 'package.json');
await m({cwd: path.dirname(filepath)});
await xoInit({cwd: path.dirname(filepath)});
t.truthy(get(JSON.parse(fs.readFileSync(filepath, 'utf8')), 'devDependencies.xo'));
});

test('installs via yarn if there\'s a lockfile', async t => {
const yarnLock = tempWrite.sync('', 'yarn.lock');
await m({cwd: path.dirname(yarnLock)});
await xoInit({cwd: path.dirname(yarnLock)});
t.regex(fs.readFileSync(yarnLock, 'utf8'), /xo/);
});

0 comments on commit 1ba26be

Please sign in to comment.