Skip to content

Commit

Permalink
#162: migrate form CommonJS to ES6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ilg-ul committed Feb 8, 2023
1 parent b15e5dd commit 5cb7e78
Show file tree
Hide file tree
Showing 30 changed files with 402 additions and 243 deletions.
5 changes: 2 additions & 3 deletions README-DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ npm outdated
Details about dependencies:

- <https://www.npmjs.com/package/@ilg/cli-start-options>
- <https://www.npmjs.com/package/@xpack/es6-promisifier>
- <https://www.npmjs.com/package/@xpack/xpm-liquid>
- <https://www.npmjs.com/package/cacache>
- <https://www.npmjs.com/package/cp-file>
Expand Down Expand Up @@ -200,7 +199,7 @@ To be accepted as a template, a project must:

- be an xPack (have a `package.json` which includes an `xpack` property
- have a property called `main` in `package.json`, pointing to a JavaScript
file that can be consumed by `require()`
file that can be consumed by `await import()` (formerly `require()`)
- the main file must export a class called `XpmInitTemplate`
- an instances of this class must have a `run()` method.
- have all dependencies bundled in (via `bundleDependencies`)
Expand All @@ -225,7 +224,7 @@ The full code is in `init.js`, but a simplified version looks like this:
context.CliError = CliError
context.CliExitCodes = CliExitCodes

const { XpmInitTemplate } = require(mainTemplatePath)
const { XpmInitTemplate } = await import(mainTemplatePath)
const xpmInitTemplate = new XpmInitTemplate(context)
const code = await xpmInitTemplate.run()
```
2 changes: 1 addition & 1 deletion bin/xpm-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
// ----------------------------------------------------------------------------

// ES6: `import { Xpm } from 'main.js'
const { XpmDev } = require('../lib/main-dev.js')
import { XpmDev } from '../lib/main-dev.js'

// ----------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion bin/xpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
// ----------------------------------------------------------------------------

// ES6: `import { Xpm } from 'main.js'
const { Xpm } = require('../lib/main.js')
import { Xpm } from '../lib/main.js'

// ----------------------------------------------------------------------------

Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@
*/

// ES6: `import { Main } from './lib/main.js'
const { Main } = require('./lib/main.js')
import mainCsj from './lib/main.js'

export const { Main } = mainCsj

// ----------------------------------------------------------------------------
// Node.js specific export definitions.

// By default, `module.exports = {}`.
// The Main class is added as a property with the same name to this object.

module.exports.Main = Main
// module.exports.Main = Main

// In ES6, it would be:
// export class Main { ... }
Expand Down
13 changes: 11 additions & 2 deletions lib/main-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

'use strict'

/* eslint valid-jsdoc: "error" */
/* eslint max-len: [ "error", 80, { "ignoreUrls": true } ] */

Expand All @@ -30,12 +31,20 @@
// ----------------------------------------------------------------------------

// https://nodejs.org/docs/latest-v12.x/api/index.htm
const path = require('path')
import path from 'path'

import { fileURLToPath } from 'url'

// ----------------------------------------------------------------------------

// ES6: `import { CliApplication, CliOptions } from 'cli-start-options'
const { CliApplication, CliOptions } = require('@ilg/cli-start-options')
// import { CliApplication, CliOptions } from '@ilg/cli-start-options'
import cliStartOptionsCsj from '@ilg/cli-start-options'

// ----------------------------------------------------------------------------

const { CliApplication, CliOptions } = cliStartOptionsCsj
const __dirname = path.dirname(fileURLToPath(import.meta.url))

// ============================================================================

Expand Down
18 changes: 13 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

'use strict'

/* eslint valid-jsdoc: "error" */
/* eslint max-len: [ "error", 80, { "ignoreUrls": true } ] */

Expand All @@ -29,20 +30,27 @@

// ----------------------------------------------------------------------------

const fs = require('fs')
import fs from 'fs'

// https://nodejs.org/docs/latest-v12.x/api/index.htm
const path = require('path')
import path from 'path'

import { fileURLToPath } from 'url'

// ----------------------------------------------------------------------------

// ES6: `import { CliApplication, CliOptions } from 'cli-start-options'
const { CliApplication, CliOptions } = require('@ilg/cli-start-options')
import cliStartOptionsCsj from '@ilg/cli-start-options'

// ----------------------------------------------------------------------------

const { CliApplication, CliOptions } = cliStartOptionsCsj
const __dirname = path.dirname(fileURLToPath(import.meta.url))

// ============================================================================

// export
class Xpm extends CliApplication {
export class Xpm extends CliApplication {
// --------------------------------------------------------------------------

/**
Expand Down Expand Up @@ -129,7 +137,7 @@ class Xpm extends CliApplication {
// By default, `module.exports = {}`.
// The current class is added as a property to this object.

module.exports.Xpm = Xpm
// module.exports.Xpm = Xpm

// In ES6, it would be:
// export class Xpm { ... }
Expand Down
14 changes: 9 additions & 5 deletions lib/utils/fs-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
*/

'use strict'

/* eslint valid-jsdoc: "error" */
/* eslint max-len: [ "error", 80, { "ignoreUrls": true } ] */

// ----------------------------------------------------------------------------

// https://nodejs.org/docs/latest-v12.x/api/index.htm
const assert = require('assert')
const fs = require('fs')
import assert from 'assert'
import fs from 'fs'
import path from 'path'

// ----------------------------------------------------------------------------

const fsPromises = fs.promises
const path = require('path')

// ----------------------------------------------------------------------------

class FsUtils {
export class FsUtils {
static async chmodRecursive ({ inputPath, readOnly, log }) {
assert(inputPath, 'mandatory inputPath')
assert(log, 'mandatory log')
Expand Down Expand Up @@ -104,7 +108,7 @@ class FsUtils {

// By default, `module.exports = {}`.
// The FsUtils class is added as a property of this object.
module.exports.FsUtils = FsUtils
// module.exports.FsUtils = FsUtils

// In ES6, it would be:
// export class FsUtils { ... }
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

// ----------------------------------------------------------------------------

function isString (x) {
export function isString (x) {
return Object.prototype.toString.call(x) === '[object String]'
}

function isObject (x) {
export function isObject (x) {
return typeof x === 'object' && !Array.isArray(x)
}

function isBoolean (x) {
export function isBoolean (x) {
return typeof x === 'boolean'
}

Expand All @@ -32,9 +32,9 @@ function isBoolean (x) {

// By default, `module.exports = {}`.
// Each function is added as a property of this object.
module.exports.isString = isString
module.exports.isObject = isObject
module.exports.isBoolean = isBoolean
// module.exports.isString = isString
// module.exports.isObject = isObject
// module.exports.isBoolean = isBoolean

// In ES6, it would be:
// import { isString, isObject, isBoolean } from 'functions.js'
Expand Down
21 changes: 14 additions & 7 deletions lib/utils/global-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,31 @@
*/

'use strict'

/* eslint valid-jsdoc: "error" */
/* eslint max-len: [ "error", 80, { "ignoreUrls": true } ] */

// ----------------------------------------------------------------------------

// https://nodejs.org/docs/latest-v12.x/api/index.htm
const assert = require('assert')
const fsPromises = require('fs').promises
const os = require('os')
const path = require('path')
import assert from 'assert'
import fs from 'fs'
import os from 'os'
import path from 'path'

// ----------------------------------------------------------------------------

// import { CliError } from '@ilg/cli-start-options'
import cliStartOptionsCsj from '@ilg/cli-start-options'

// ----------------------------------------------------------------------------

const { CliError } = require('@ilg/cli-start-options')
const { CliError } = cliStartOptionsCsj
const fsPromises = fs.promises

// ============================================================================

class GlobalConfig {
export class GlobalConfig {
// --------------------------------------------------------------------------

constructor () {
Expand Down Expand Up @@ -142,7 +149,7 @@ class GlobalConfig {

// By default, `module.exports = {}`.
// The Config class is added as a property of this object.
module.exports.GlobalConfig = GlobalConfig
// module.exports.GlobalConfig = GlobalConfig

// In ES6, it would be:
// export class GlobalConfig { ... }
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
*/

'use strict'

/* eslint valid-jsdoc: "error" */
/* eslint max-len: [ "error", 80, { "ignoreUrls": true } ] */

// ----------------------------------------------------------------------------

// https://nodejs.org/docs/latest-v12.x/api/index.html
const assert = require('assert')
import assert from 'assert'

// https://www.npmjs.com/package/semver
const semver = require('semver')
import semver from 'semver'

// ============================================================================

class Policies {
export class Policies {
constructor (minVersion, context) {
this.minVersion = minVersion || '0.0.0'
assert(context, 'mandatory context')
Expand All @@ -44,7 +45,7 @@ class Policies {

// By default, `module.exports = {}`.
// The Policies class is added as a property of this object.
module.exports.Policies = Policies
// module.exports.Policies = Policies

// In ES6, it would be:
// export class Policies { ... }
Expand Down
11 changes: 6 additions & 5 deletions lib/utils/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
*/

'use strict'

/* eslint valid-jsdoc: "error" */
/* eslint max-len: [ "error", 80, { "ignoreUrls": true } ] */

// ----------------------------------------------------------------------------

const util = require('util')

// https://nodejs.org/docs/latest-v12.x/api/index.htmchild_process.html#child_process_child_process_spawn_command_args_options
// const { spawn } = require('child_process')

// https://www.npmjs.com/package/cross-spawn
const spawn = require('cross-spawn')
import spawn from 'cross-spawn'

import util from 'util'

// ============================================================================

Expand Down Expand Up @@ -106,7 +107,7 @@ const promiseSpawnUid = (cmd, args, opts, extra) => {
return p
}

class Spawn {
export class Spawn {
spawnShellPromise (cmdString, opts) {
if (!opts.stdio) {
opts.stdio = 'inherit'
Expand Down Expand Up @@ -191,7 +192,7 @@ class Spawn {

// By default, `module.exports = {}`.
// The current class is added as a property of this object.
module.exports.Spawn = Spawn
// module.exports.Spawn = Spawn

// In ES6, it would be:
// export class Spawn { ... }
Expand Down
Loading

0 comments on commit 5cb7e78

Please sign in to comment.