Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 13, 2021
1 parent e6cb50d commit 95a6401
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 89 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
6 changes: 3 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';
const createXo = require('.');
import process from 'node:process';
import createXo from './index.js';

createXo({
args: process.argv.slice(2)
args: process.argv.slice(2),
});
49 changes: 26 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
'use strict';
const path = require('path');
const minimist = require('minimist');
const arrify = require('arrify');
const argv = require('the-argv');
const pathExists = require('path-exists');
const readPkgUp = require('read-pkg-up');
const writePkg = require('write-pkg');
const execa = require('execa');
const hasYarn = require('has-yarn');
import process from 'node:process';
import path from 'node:path';
import fs from 'node:fs';
import minimist from 'minimist';
import {readPackageUpSync} from 'read-pkg-up';
import {writePackageSync} from 'write-pkg';
import execa from 'execa';
import hasYarn from 'has-yarn';

const DEFAULT_TEST_SCRIPT = 'echo "Error: no test specified" && exit 1';

const PLURAL_OPTIONS = [
'env',
'global',
'ignore'
'ignore',
];

const CONFIG_FILES = [
Expand All @@ -26,7 +24,7 @@ const CONFIG_FILES = [
'.jshintrc',
'.jscsrc',
'.jscs.json',
'.jscs.yaml'
'.jscs.yaml',
];

const buildTestScript = test => {
Expand All @@ -43,7 +41,7 @@ const buildTestScript = test => {
};

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

if (files.length === 0) {
return;
Expand All @@ -52,33 +50,38 @@ const warnConfigFile = packageCwd => {
console.log(`${files.join(' & ')} can probably be deleted now that you're using XO.`);
};

module.exports = async (options = {}) => {
const packageResult = readPkgUp.sync({
export default async function createXo(options = {}) {
const {
packageJson = {},
path: packagePath = path.resolve(options.cwd || '', 'package.json'),
} = readPackageUpSync({
cwd: options.cwd,
normalize: false
normalize: false,
}) || {};
const packageJson = packageResult.package || {};
const packagePath = packageResult.path || path.resolve(options.cwd || '', 'package.json');

const packageCwd = path.dirname(packagePath);

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.test = buildTestScript(packageJson.scripts.test);

const cli = minimist(options.args || argv());
const cli = minimist(options.args || process.argv.slice(2));
delete cli._;

for (const option of PLURAL_OPTIONS) {
if (cli[option]) {
cli[`${option}s`] = arrify(cli[option]);
cli[`${option}s`] = [cli[option]].flat();
delete cli[option];
}
}

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

writePkg.sync(packagePath, packageJson);
writePackageSync(packagePath, packageJson);

const post = () => {
warnConfigFile(packageCwd);
Expand Down Expand Up @@ -107,4 +110,4 @@ module.exports = async (options = {}) => {

await execa('npm', ['install', '--save-dev', 'xo'], {cwd: packageCwd});
post();
};
}
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
"description": "Add XO to your project",
"license": "MIT",
"repository": "xojs/create-xo",
"funding": "https://github.com/xojs/create-xo?sponsor=1",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"bin": "./cli.js",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
},
"bin": "cli.js",
"files": [
"index.js",
"cli.js"
Expand All @@ -32,19 +35,16 @@
"xo"
],
"dependencies": {
"arrify": "^2.0.1",
"execa": "^1.0.0",
"has-yarn": "^2.1.0",
"minimist": "^1.1.3",
"path-exists": "^4.0.0",
"read-pkg-up": "^6.0.0",
"the-argv": "^1.0.0",
"write-pkg": "^4.0.0"
"execa": "^5.1.1",
"has-yarn": "^3.0.0",
"minimist": "^1.2.5",
"read-pkg-up": "^9.0.0",
"write-pkg": "^5.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"dot-prop": "^5.1.0",
"temp-write": "^4.0.0",
"xo": "^0.25.3"
"ava": "^3.15.0",
"dot-prop": "^6.0.1",
"temp-write": "^5.0.0",
"xo": "^0.45.0"
}
}
26 changes: 11 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@

> Add [XO](https://github.com/xojs/xo) to your project

## CLI

```
$ npm init xo [options]
```sh
npm init xo [options]
```

Example:

```sh
npm init xo --space --no-semicolon
```
$ npm init xo --space --no-semicolon
```


## API

### Usage

```js
const createXo = require('create-xo');
import createXo from 'create-xo';

(async () => {
await createXo();
})();
await createXo();
```

### createXo(options?)
Expand All @@ -38,19 +34,19 @@ Type: `object`

#### cwd

Type: `string`<br>
Type: `string`\
Default: `process.cwd()`

Current working directory.
The current working directory.

#### args

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

Options to put in [XO's config](https://www.npmjs.com/package/xo#config) in `package.json`.
The options to put in [XO's config](https://www.npmjs.com/package/xo#config) in `package.json`.

For instance, with the arguments `['--space', '--env=node']` the following will be put in `package.json`:
For instance, with the arguments `['--space', '--env=node']`, the following will be put in `package.json`:

```json
{
Expand Down

0 comments on commit 95a6401

Please sign in to comment.