Skip to content

Commit

Permalink
build(esm): migrate to ES Modules
Browse files Browse the repository at this point in the history
Migrate to ESM to allow updating packages with no more CJS support such as supports-color or
terminal-link. Don't wait for jprichardson/node-fs-extra#746.

BREAKING CHANGE: remove support to Node 10
  • Loading branch information
atao60 committed Jun 29, 2021
1 parent e6dec0d commit f4a0e7b
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 12.20
registry-url: https://npm.pkg.github.com/
scope: '@atao60'
- run: npm ci
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
Expand Up @@ -30,19 +30,25 @@
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.ignoreWords": [
"anyfileext",
"apigithub",
"apigithuburl",
"browserslistrc",
"commitlint",
"currentinputs",
"def",
"dirpath",
"florian",
"fullcheck",
"fullcopyright",
"fulltest",
"fulltext",
"gerth",
"gitquery",
"globaltest",
"hassy",
"ignorecase",
"job",
"js",
"khaled",
"kreuzer",
"logerror",
Expand All @@ -61,6 +67,7 @@
"preinstall",
"readdirp",
"refname",
"repos",
"rootpath",
"shortpath",
"startingat",
Expand Down
24 changes: 16 additions & 8 deletions CONTRIBUTING.md
Expand Up @@ -107,10 +107,11 @@ At the moment there is no CI/Build configuration on [Github](https://github.com)
## Prerequisites

* [Git](https://git-scm.com/)
* [Node.js](https://nodejs.org/)
* [Npm](https://www.npmjs.com/) - comes with Node.js
* [Npx](https://github.com/npm/npx#readme) - comes with Node.js
* [Node.js](https://nodejs.org/) - *at least version 12.20*
* [Npm](https://www.npmjs.com/) - *comes with Node.js*
* [Npx](https://github.com/npm/npx#readme) - *comes with Node.js*
* [Docker](https://www.docker.com/)
* [nvm](https://github.com/nvm-sh/nvm) - *optional*

and possibly:
* a [GitHub account](https://github.com/)
Expand All @@ -126,6 +127,8 @@ git --version

docker --version # check if BuildKit can be used, i.e. with Docker 18.09 or higher

nvm --version # required only if you need to install Node with at least version 12.20 or higher

npm list -g --depth 0 2>&1 | grep fse-cli # (°°)

```
Expand All @@ -138,6 +141,8 @@ npm list -g --depth 0 2>&1 | grep fse-cli # (°°)
### Fork

```bash
node --version ### check Node.js is installed with at least version 12.20

sudo npm uninstall -g @atao60/fse-cli ### if needed; required to avoid any issue with `npm link`, see below

git clone https://github.com/atao60/fse-cli.git atao60-fse-cli
Expand Down Expand Up @@ -213,17 +218,20 @@ npm start ### will rebuild and test after each code change
```
### Iterate tests over main versions of Node.js

To ensure this package `@atao60/fse-cli` is compatible with all major versions of [Node.js](https://nodejs.org), a [Docker](https://www.docker.com/) image with the last release of each of them is used to run the tests:
To ensure this package `@atao60/fse-cli` is compatible with all major versions of [Node.js](https://nodejs.org), a [Docker](https://www.docker.com/)(°) image with the last release of each of them is used to run the tests:

```bash
./make.sh nodecheckall
```
The checked versions of [Node.js](https://nodejs.org) are from 10 to 16.
The checked releases of [Node.js](https://nodejs.org) are from 12 to last available one as long as a version compatible with [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) is available(°°).

> As soon as [Docker](https://www.docker.com/) is installed, some Bourne shell or compatible should be available, allowing running `make.sh`.
> (°) As soon as [Docker](https://www.docker.com/) is installed, some Bourne shell or compatible one should be available, allowing cross-platform running of `make.sh`.
>[EOL](https://en.wikipedia.org/wiki/End-of-life_product) of `Node.js` 10 was the 30th April 2021, see [Node.js' Releases](https://github.com/nodejs/Release).
Even if [Docker Official Images for Node.js](https://hub.docker.com/_/node) doesn't support any more versions below 12, older versions are still available.
> (°°) Already reached [EOL](https://en.wikipedia.org/wiki/End-of-life_product) of [Node.js' Releases](https://github.com/nodejs/Release) are:
- 10: 30th April 2021,
- 13: 1st of june 2020,
- 15: 1st of june 2021.
As release 15 is compatible with `ESM`, it's not been removed from checking yet.

> Don't forget to regularly check and clean up disk space used by [Docker](https://www.docker.com/): `docker system df` and `docker system prune` are your friends here!
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@ Welcome to @atao/fse-cli

<img src="https://raw.githubusercontent.com/googlefonts/noto-emoji/v2018-08-10-unicode11/svg/emoji_u1f3d7.svg" width="40" /> A [CLI](https://en.wikipedia.org/wiki/Command-line_interface) for [fs-extra](https://github.com/jprichardson/node-fs-extra).

> Releases (0.0.x) will be the last ones to support version 10 of [Node.js](https://nodejs.org).
> Releases (0.0.x) will be the last ones to support LTS release [10](https://nodejs.org/download/release/v10.24.1/) of [Node.js](https://nodejs.org). Moreover the releases [11](https://nodejs.org/download/release/v11.15.0/) and [13](https://nodejs.org/download/release/v13.14.0/) are also no more supported.
## 💡 Rational

Everyone needs simple file system operations like copy, remove, clean, ... that can be used from the terminal or via scripts.
Expand Down
2 changes: 1 addition & 1 deletion USAGE
Expand Up @@ -38,7 +38,7 @@ or the environment variable 'FSE_CLI_QUIET=true'.
`fse remove --quiet <directory's path>`

* touch Ensures that the file exists. If the file that is requested to be created is in directories that do not exist,
these directories are created. If the file already exists, it is NOT MODIFIED."
these directories are created. If the file already exists, it is NOT MODIFIED.
Aliases: ***ensureFile***, ***touch***

`fse touch --quiet <new file's path>`
Expand Down
4 changes: 2 additions & 2 deletions babel.config.cjs
@@ -1,5 +1,5 @@
const targets = {
node: "10.15.3" // See also engines.node in package.json
node: "12.20.0" // See also engines.node in package.json
// With @atao/fse-cli, indeed no '.browserslistrc'
};

Expand Down Expand Up @@ -45,7 +45,7 @@ const presets = [
},
// Now Babel defaults 'modules' to 'auto', not any more to 'commonjs' (which won’t tree-shake.)
// So no more need to setup 'modules' to 'false':
// modules: false,
modules: false,
targets
}
]
Expand Down
2 changes: 1 addition & 1 deletion bin/fse.js
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require('../dist/index.js');
import '../dist/index.js';
2 changes: 1 addition & 1 deletion make.sh
Expand Up @@ -5,7 +5,7 @@ MAKE=$0
OWNER=atao60
NAME=fse-cli
VERSION=${2:-10.15.3}
ALL_MAIN_VERSIONS="10 11 12 13 14 15 16"
ALL_MAIN_VERSIONS="12 14 15 16"

TEST_IMAGE_NAME=$OWNER/$NAME-test

Expand Down
50 changes: 41 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -2,8 +2,9 @@
"name": "@atao60/fse-cli",
"version": "0.0.63",
"description": "A cli for fs-extra",
"main": "./dist/index.js",
"exports": "./dist/index.js",
"types": "./dist/index-types.d.ts",
"type": "module",
"bin": {
"fse": "./bin/fse.js",
"fse-cli": "./bin/fse.js",
Expand Down Expand Up @@ -126,8 +127,8 @@
"author": "Pierre Raoul <atao60.dev@gmail.com>",
"license": "MIT",
"engines": {
"node": ">=10.15.3",
"npm": ">=6.4.1"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0",
"npm": ">=6.14.11"
},
"dependencies": {
"@babel/runtime": "^7.13.10",
Expand All @@ -139,7 +140,7 @@
"inquirer": "^8.0.0",
"regenerator-runtime": "^0.13.7",
"source-map-support": "^0.5.19",
"terminal-link": "^2.1.1",
"terminal-link": "^3.0.0",
"tslib": "^2.2.0"
},
"devDependencies": {
Expand Down Expand Up @@ -189,7 +190,7 @@
"npm-force-resolutions": "0.0.10",
"readdirp": "^3.6.0",
"standard-version": "^9.2.0",
"supports-color": "^8.1.1",
"supports-color": "^9.0.1",
"ts-node": "^10.0.0",
"typescript": "^4.2"
},
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Expand Up @@ -2,11 +2,14 @@ import arg from 'arg';
import chalk from 'chalk';
import inquirer from 'inquirer';
const { prompt } = inquirer;
import { basename, join } from 'path';
import { basename, dirname, join } from 'path';
import { exit } from 'process';
import { fileURLToPath } from 'url';

import { JobDef } from './job-def.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

// !!! ⚠️ Don't forget to update the section 'bin' of package.json for any change of jobLinks ⚠️ !!!
const jobLinks = Object.freeze({
copy: 'copy',
Expand Down
7 changes: 5 additions & 2 deletions src/tasks/help.ts
@@ -1,12 +1,15 @@
import chalk, { Chalk } from 'chalk';
import fse from 'fs-extra';
const { existsSync, readFileSync } = fse;
import { join } from 'path';
import chalk, { Chalk } from 'chalk';
import { dirname, join } from 'path';
import terminalLink from 'terminal-link';
import { fileURLToPath } from 'url';

import { job as version } from './version.js';
import { info } from '../logger.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

const versionDef = {
name: 'help',
spec: {},
Expand Down
5 changes: 4 additions & 1 deletion src/tasks/version.ts
@@ -1,9 +1,12 @@
import fse from 'fs-extra';
const { existsSync, readFileSync } = fse;
import { join } from 'path';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

import * as logger from '../logger.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

const versionDef = {
name: 'version',
spec: {},
Expand Down
4 changes: 2 additions & 2 deletions src/tsconfig.json
Expand Up @@ -8,11 +8,11 @@
"strict": true,
"outDir": "../dist",
"baseUrl": "",
"module": "commonjs",
"module": "ES2020",
"moduleResolution": "node",
"lib": [
"es2018",
"dom"
]
}
}
}
5 changes: 4 additions & 1 deletion src/wrapper.ts
@@ -1,4 +1,7 @@
import { join } from 'path';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));

const tasksSubDir = 'tasks';

Expand Down
7 changes: 5 additions & 2 deletions test/cli.test.ts
Expand Up @@ -3,14 +3,17 @@ import gfs from 'graceful-fs';
const { closeSync, existsSync, mkdtempSync, mkdirSync, openSync, readFileSync, rmdirSync, statSync, unlinkSync, writeSync } = gfs;
import mocha from 'mocha';
const { describe, it } = mocha;
import { join } from 'path';
import { dirname, join } from 'path';
import { env } from 'process';
import { format as printf } from 'util';
import sc from 'supports-color';
const { stdout: supportsColorStdout } = sc;
import { fileURLToPath } from 'url';
import { format as printf } from 'util';

import { execute as run } from './cmd.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

const LIB_DIR = join(__dirname, env.APP_CODE_PATH || '../../dist');
// os.tmpdir() is not used here, to stay inside project folder
const TMP_DIR = join(__dirname, env.PROJECT_TARGET_PATH || '../.tmp-dir');
Expand Down
8 changes: 5 additions & 3 deletions test/tsconfig.json
Expand Up @@ -7,12 +7,14 @@
"allowSyntheticDefaultImports": true,
"outDir": "../dist",
"baseUrl": "",
"module": "commonjs",
"module": "ES2020",
"moduleResolution": "node",
"lib": [
"es2018",
"dom"
]
},
"include": ["**/*.test.ts"]
}
"include": [
"**/*.test.ts"
]
}
3 changes: 3 additions & 0 deletions tools/package.json
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

1 comment on commit f4a0e7b

@atao60
Copy link
Owner Author

@atao60 atao60 commented on f4a0e7b Jul 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I migrated to ESM with the amazing help of Sindre Sorhus' docs:

But I agree with @StarpTech's comment: I shouldn't have been obliged to migrate to EMS only because some dependencies have migrated to EMS only. They should have keep offering the choice between EMS and CommonJS. See Node Modules at War: Why CommonJS and ES Modules Can’t Get Along, Dan Fabulich, Aug 5, 2020.

Please sign in to comment.