Skip to content

Commit 65e2c05

Browse files
committed
chore!: use pure ES modules
1 parent c79bc1a commit 65e2c05

21 files changed

+216
-232
lines changed

.eslintrc.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ plugins: [node, prettier]
22
env:
33
node: true
44
es6: true
5+
parserOptions:
6+
ecmaVersion: 2021
7+
sourceType: module
58
extends:
69
- plugin:import/errors
710
- plugin:import/warnings

ava.config.js

-4
This file was deleted.

init/bin.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env node
22
// eslint-disable-next-line node/shebang
3-
const { exit } = require('process')
3+
import { exit, argv } from 'process'
44

5-
const yargs = require('yargs')
6-
const filterObj = require('filter-obj')
5+
import yargs from 'yargs'
6+
import { hideBin } from 'yargs/helpers'
7+
import filterObj from 'filter-obj'
78

8-
const { init } = require('./main.js')
9-
const { VARIABLES } = require('./variables/main.js')
9+
import { init } from './main.js'
10+
import { VARIABLES } from './variables/main.js'
1011

1112
// CLI entry point of `npm run init`
1213
const runCli = async function () {
@@ -23,7 +24,7 @@ const runCli = async function () {
2324

2425
// Parse CLI flags
2526
const parseFlags = function () {
26-
return yargs.options(FLAGS).usage(USAGE).parse()
27+
return yargs(hideBin(argv)).options(FLAGS).usage(USAGE).parse()
2728
}
2829

2930
// Retrieve CLI flags
@@ -36,10 +37,12 @@ const getVariableFlag = function ({
3637
description,
3738
default: defaultValue,
3839
}) {
40+
const defaultDescription =
41+
typeof defaultValue === 'string' ? `\nDefault: ${defaultValue}` : ''
3942
return {
4043
[name]: {
4144
string: true,
42-
describe: `${description}.\nDefault: ${defaultValue}`,
45+
describe: `${description}.${defaultDescription}`,
4346
},
4447
}
4548
}

init/clean.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
const { readFile, writeFile } = require('fs')
2-
const { promisify } = require('util')
1+
import { readFile, writeFile } from 'fs/promises'
2+
import { fileURLToPath } from 'url'
33

4-
const del = require('del')
5-
const omit = require('omit.js').default
6-
const filterObj = require('filter-obj')
4+
import del from 'del'
5+
import omit from 'omit.js'
6+
import filterObj from 'filter-obj'
77

8-
const PACKAGE_ROOT = `${__dirname}/..`
8+
const PACKAGE_ROOT = fileURLToPath(new URL('..', import.meta.url))
99
const SCRIPTS_DIR = `${PACKAGE_ROOT}/init`
1010
const PACKAGE_JSON = `${PACKAGE_ROOT}/package.json`
1111

12-
const pReadFile = promisify(readFile)
13-
const pWriteFile = promisify(writeFile)
14-
1512
// Remove all files, properties and logic needed by `npm run init` once
1613
// `npm run init` is done.
17-
const cleanRepo = async function () {
14+
export const cleanRepo = async function () {
1815
await Promise.all([del(SCRIPTS_DIR, { force: true }), cleanPackageJson()])
1916
}
2017

2118
// Remove `npm run init` in `package.json` and all `devDependencies`.
2219
const cleanPackageJson = async function () {
23-
const content = await pReadFile(PACKAGE_JSON, 'utf8')
24-
const { scripts, dependencies, devDependencies, ...packageJson } = JSON.parse(
25-
content,
26-
)
20+
const content = await readFile(PACKAGE_JSON, 'utf8')
21+
const { scripts, dependencies, devDependencies, ...packageJson } =
22+
JSON.parse(content)
2723

28-
const scriptsA = omit(scripts, ['init'])
24+
const scriptsA = omit.default(scripts, ['init'])
2925
const devDependenciesA = filterObj(devDependencies, shouldKeepDevDependency)
3026
const packageJsonA = {
3127
...packageJson,
@@ -35,7 +31,7 @@ const cleanPackageJson = async function () {
3531
}
3632

3733
const contentA = JSON.stringify(packageJsonA, null, 2)
38-
await pWriteFile(PACKAGE_JSON, contentA)
34+
await writeFile(PACKAGE_JSON, contentA)
3935
}
4036

4137
// Remove devDependencies used only for initialization
@@ -57,5 +53,3 @@ const DEV_DEPENDENCIES = [
5753
'prettier',
5854
'release-it',
5955
]
60-
61-
module.exports = { cleanRepo }

init/copy.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
const cpy = require('cpy')
1+
import cpy from 'cpy'
22

3-
const PACKAGE_ROOT = `${__dirname}/..`
3+
import { fileURLToPath } from 'url'
4+
5+
const INIT_DIR = fileURLToPath(new URL('.', import.meta.url))
6+
const PACKAGE_ROOT = fileURLToPath(new URL('..', import.meta.url))
47

58
// Copy some files during initialization
6-
const copyFiles = async function () {
7-
const files = FILES.map((file) => `${__dirname}/${file}`)
9+
export const copyFiles = async function () {
10+
const files = FILES.map((file) => `${INIT_DIR}/${file}`)
811
await cpy(files, PACKAGE_ROOT)
912
}
1013

1114
const FILES = ['README.md']
12-
13-
module.exports = { copyFiles }

init/main.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const execa = require('execa')
2-
const { red } = require('chalk')
1+
import execa from 'execa'
2+
import chalk from 'chalk'
33

4-
const { getOptions } = require('./options.js')
5-
const { copyFiles } = require('./copy.js')
6-
const { applyTemplates } = require('./template.js')
7-
const { cleanRepo } = require('./clean.js')
8-
// const { createSite } = require('./site.js')
4+
import { getOptions } from './options.js'
5+
import { copyFiles } from './copy.js'
6+
import { applyTemplates } from './template.js'
7+
import { cleanRepo } from './clean.js'
8+
// import { createSite } from './site.js'
99

1010
// `npm run init` main logic.
1111
// Initialize/scaffold the template repository.
12-
const init = async function (options) {
12+
export const init = async function (options) {
1313
const { variables } = await getOptions(options)
1414

1515
try {
@@ -21,7 +21,7 @@ const init = async function (options) {
2121
await execa.command('git commit -m Init')
2222
// Revert changes on errors
2323
} catch (error) {
24-
console.error(red('Error: Initialization failed.'))
24+
console.error(chalk.red('Error: Initialization failed.'))
2525
await execa.command('git reset --hard')
2626
await npmInstall()
2727
throw error
@@ -35,5 +35,3 @@ const npmInstall = async function () {
3535
stdio: 'inherit',
3636
})
3737
}
38-
39-
module.exports = { init }

init/options.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
const { getVariables } = require('./variables/main.js')
1+
import { getVariables } from './variables/main.js'
22

33
// Retrieve options and apply default values
4-
const getOptions = async function (options = {}) {
4+
export const getOptions = async function (options = {}) {
55
const optionsA = { ...DEFAULT_OPTIONS, ...options }
66
const variables = await getVariables(optionsA)
77
return { ...optionsA, variables }
88
}
99

1010
const DEFAULT_OPTIONS = {}
11-
12-
module.exports = { getOptions }

init/site.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const execa = require('execa')
2-
const { bold } = require('chalk')
3-
const stripAnsi = require('strip-ansi')
1+
import execa from 'execa'
2+
import chalk from 'chalk'
3+
import stripAnsi from 'strip-ansi'
44

5-
const { getRepo } = require('./variables/repo.js')
6-
const { applyTemplates } = require('./template.js')
5+
import { getRepo } from './variables/repo.js'
6+
import { applyTemplates } from './template.js'
77

88
// Create a Netlify Site for the new repository
9-
const createSite = async function ({ name }) {
9+
export const createSite = async function ({ name }) {
1010
try {
11-
console.log(bold(`\nCreating a new Netlify Site...\n`))
11+
console.log(chalk.bold(`\nCreating a new Netlify Site...\n`))
1212
await login()
1313
const username = await getUsername()
1414
const siteId = await createNewSite(name, username)
@@ -60,9 +60,7 @@ const createNewSite = async function (name, username) {
6060
}
6161

6262
const accountSlug = getAccountSlug(username)
63-
const {
64-
all: allA,
65-
} = await execa.command(
63+
const { all: allA } = await execa.command(
6664
`netlify sites:create --name netlify-plugin-${name} ${accountSlug}`,
6765
{ all: true },
6866
)
@@ -93,5 +91,3 @@ const getAccountSlug = function (username) {
9391
}
9492
return `--account-slug=${username}`
9593
}
96-
97-
module.exports = { createSite }

init/template.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
const { readFile, writeFile } = require('fs')
2-
const { promisify } = require('util')
1+
import { readFile, writeFile } from 'fs/promises'
2+
import { fileURLToPath } from 'url'
33

4-
const fastGlob = require('fast-glob')
4+
import fastGlob from 'fast-glob'
55

6-
const PACKAGE_ROOT = `${__dirname}/..`
6+
const PACKAGE_ROOT = fileURLToPath(new URL('..', import.meta.url))
77
// Files that have template variables
88
const TEMPLATES = ['*.{json,md}', '{src,.github}/**']
99

10-
const pReadFile = promisify(readFile)
11-
const pWriteFile = promisify(writeFile)
12-
1310
// Substitute template variables {{var}} in all source files
14-
const applyTemplates = async function (variables) {
11+
export const applyTemplates = async function (variables) {
1512
const templates = TEMPLATES.map(addPackageRoot)
1613
const files = await fastGlob(templates)
1714
await Promise.all(files.map((file) => applyTemplate(file, variables)))
@@ -22,9 +19,9 @@ const addPackageRoot = function (path) {
2219
}
2320

2421
const applyTemplate = async function (file, variables) {
25-
const content = await pReadFile(file, 'utf8')
22+
const content = await readFile(file, 'utf8')
2623
const contentA = replaceVariables(content, variables)
27-
await pWriteFile(file, contentA)
24+
await writeFile(file, contentA)
2825
}
2926

3027
const replaceVariables = function (content, variables) {
@@ -51,5 +48,3 @@ const replaceVariable = function (content, [name, value]) {
5148

5249
const DUMMY_NAME = 'netlify-plugin-example'
5350
const NODE_VERSION = '^12.20.0 || ^14.14.0 || >=16.0.0'
54-
55-
module.exports = { applyTemplates }

init/variables/author.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const username = require('username')
2-
const execa = require('execa')
1+
import username from 'username'
2+
import execa from 'execa'
33

4-
const { trim } = require('./trim.js')
4+
import { trim } from './trim.js'
55

66
// {{author}} template variable
7-
const AUTHOR_VARIABLE = {
7+
export const AUTHOR_VARIABLE = {
88
name: 'author',
99
description: 'Author name',
1010
// Try to guess current username
@@ -20,5 +20,3 @@ const AUTHOR_VARIABLE = {
2020
},
2121
filter: trim,
2222
}
23-
24-
module.exports = { AUTHOR_VARIABLE }

init/variables/description.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
const { trimTitleize } = require('./trim.js')
1+
import { trimTitleize } from './trim.js'
22

33
// {{description}} template variable
4-
const DESCRIPTION_VARIABLE = {
4+
export const DESCRIPTION_VARIABLE = {
55
name: 'description',
66
description: 'Description',
77
default: 'Example description',
88
filter: trimTitleize,
99
}
10-
11-
module.exports = { DESCRIPTION_VARIABLE }

init/variables/email.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const { validate: validateEmail } = require('email-validator')
2-
const execa = require('execa')
1+
import { validate as validateEmail } from 'email-validator'
2+
import execa from 'execa'
33

4-
const { trim } = require('./trim.js')
4+
import { trim } from './trim.js'
55

66
// {{email}} template variable
7-
const EMAIL_VARIABLE = {
7+
export const EMAIL_VARIABLE = {
88
name: 'email',
99
description: 'Author email address',
1010
// Try guessing current user's development email
@@ -25,5 +25,3 @@ const EMAIL_VARIABLE = {
2525
}
2626
},
2727
}
28-
29-
module.exports = { EMAIL_VARIABLE }

init/variables/license.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const spdxLicenseList = require('spdx-license-list')
2-
const fuzzy = require('fuzzy')
1+
import spdxLicenseList from 'spdx-license-list'
2+
import fuzzy from 'fuzzy'
33

44
// {{license}} template variable.
55
// Autocompleted from the list of valid SPDX licenses.
6-
const LICENSE_VARIABLE = {
6+
export const LICENSE_VARIABLE = {
77
type: 'autocomplete',
88
name: 'license',
99
description: 'Software license',
@@ -21,5 +21,3 @@ const LICENSES = [
2121
'BSD-3-Clause',
2222
...Object.keys(spdxLicenseList),
2323
]
24-
25-
module.exports = { LICENSE_VARIABLE }

init/variables/main.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
const inquirer = require('inquirer')
2-
const inquirerAutocomplete = require('inquirer-autocomplete-prompt')
3-
const { bold } = require('chalk')
1+
import inquirer from 'inquirer'
2+
import inquirerAutocomplete from 'inquirer-autocomplete-prompt'
3+
import chalk from 'chalk'
44

5-
const { NAME_VARIABLE } = require('./name.js')
6-
const { DESCRIPTION_VARIABLE } = require('./description.js')
7-
const { AUTHOR_VARIABLE } = require('./author.js')
8-
const { EMAIL_VARIABLE } = require('./email.js')
9-
const { LICENSE_VARIABLE } = require('./license.js')
10-
const { REPO_VARIABLE } = require('./repo.js')
11-
const { NODE_VERSION_VARIABLE } = require('./node_version.js')
5+
import { NAME_VARIABLE } from './name.js'
6+
import { DESCRIPTION_VARIABLE } from './description.js'
7+
import { AUTHOR_VARIABLE } from './author.js'
8+
import { EMAIL_VARIABLE } from './email.js'
9+
import { LICENSE_VARIABLE } from './license.js'
10+
import { REPO_VARIABLE } from './repo.js'
11+
import { NODE_VERSION_VARIABLE } from './node_version.js'
1212

1313
inquirer.registerPrompt('autocomplete', inquirerAutocomplete)
1414

1515
// Retrieve all template variables from either options or CLI interactive prompt
16-
const getVariables = async function (options) {
17-
console.log(bold('\nWhich Netlify Build plugin would you like to create?\n'))
16+
export const getVariables = async function (options) {
17+
console.log(
18+
chalk.bold('\nWhich Netlify Build plugin would you like to create?\n'),
19+
)
1820

1921
const questions = VARIABLES.filter(
2022
({ name }) => options[name] === undefined,
@@ -25,7 +27,7 @@ const getVariables = async function (options) {
2527
}
2628

2729
// Each template variable
28-
const VARIABLES = [
30+
export const VARIABLES = [
2931
NAME_VARIABLE,
3032
DESCRIPTION_VARIABLE,
3133
AUTHOR_VARIABLE,
@@ -78,5 +80,3 @@ const getCurrentYear = function () {
7880
}
7981

8082
const YEAR_BASE = 1900
81-
82-
module.exports = { getVariables, VARIABLES }

0 commit comments

Comments
 (0)