Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to ES Module #8

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions commands/archive.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Archive command: Create a zip package of the project
*/
const path = require('path')
const url = require('url')
const glob = require('fast-glob')
const fs = require('fs-extra')
const { Zip } = require('zip-lib')
const fileExists = require('../utils/fileExists')

module.exports = async function archive({ config }) {
import path from 'path'
import url from 'url'
import glob from 'fast-glob'
import fs from 'fs-extra'
import { Zip } from 'zip-lib'
import fileExists from '../utils/fileExists.js'

export default async function archive({ config }) {
if (!config.archive) {
console.log('Required property "archive" in tangible.config.js')
console.log(`Example:
Expand Down
14 changes: 6 additions & 8 deletions commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Build for production
*/

const path = require('path')
const fs = require('fs/promises')
const rollup = require('rollup')
const getFileSize = require('../utils/getFileSize')
const displayError = require('../utils/displayError')
import path from 'path'
import fs from 'fs/promises'
import * as rollup from 'rollup'
import getFileSize from '../utils/getFileSize.js'
import displayError from '../utils/displayError.js'

async function build(props) {
export default async function build(props) {
const { config, task, inputOptions, outputOptions } = props

// Custom build task
Expand Down Expand Up @@ -50,5 +50,3 @@ async function build(props) {
})
}
}

module.exports = build
4 changes: 2 additions & 2 deletions commands/bun-or.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* bun-or command: Run with bun or given fallback such as node, npm, npx
*/
const { execSync, spawn } = require('node:child_process')
import { execSync, spawn } from 'node:child_process'

function run(command, args, silenceError = false) {
return new Promise((resolve, reject) => {
Expand All @@ -13,7 +13,7 @@ function run(command, args, silenceError = false) {
})
}

module.exports = async function bunOr(props = {}) {
export default async function bunOr(props = {}) {

const {
argv = process.argv
Expand Down
12 changes: 5 additions & 7 deletions commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* @see https://rollupjs.org/guide/en/#rollupwatch
*/

const path = require('path')
const rollup = require('rollup')
const onExit = require('../utils/onExit')
const displayError = require('../utils/displayError')
import path from 'path'
import * as rollup from 'rollup'
import onExit from '../utils/onExit.js'
import displayError from '../utils/displayError.js'

async function dev(props) {
export default async function dev(props) {
const { config, task, inputOptions, outputOptions, reloader } = props

// Custom build task
Expand Down Expand Up @@ -75,5 +75,3 @@ async function dev(props) {
})
})
}

module.exports = dev
28 changes: 15 additions & 13 deletions commands/format.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
const path = require('path')
const glob = require('fast-glob')
const fs = require('fs-extra')
import path from 'path'
import glob from 'fast-glob'
import fs from 'fs-extra'
import { fileURLToPath } from 'url'
import run from '../utils/run.js'

const run = require('../utils/run')
const __dirname = path.dirname(fileURLToPath(import.meta.url))

const prettierIgnorePath = path.resolve(
path.join(__dirname, '..', 'config', '.prettierignore')
path.join(__dirname, '..', 'config', '.prettierignore'),
)

let phpBeautify

async function format({ config, lint = false }) {
export default async function format({ config, lint = false }) {
if (!config.format) {
const { homepage } = require('../package.json')
const { homepage } = await fs.readJson(
path.join(__dirname, '../package.json')
)

console.log(
`Format command requires the "format" property in the config file

Documentation: ${homepage}#format
`
`,
)
return
}
Expand Down Expand Up @@ -89,7 +93,7 @@ Documentation: ${homepage}#format
if (lint) continue // No lint for JS, Sass, etc.

prettierFiles.push(
...filesByType[type].map((f) => f.replace(/"/g, '"')) // Escape quotes just in case
...filesByType[type].map((f) => f.replace(/"/g, '"')), // Escape quotes just in case
)
continue
}
Expand All @@ -103,7 +107,7 @@ Documentation: ${homepage}#format
hasPhp = true
} catch (e) {
console.log(
`PHP Beautify is now optional.\n\nPlease run: npm install --save-dev @tangible/php-beautify\n`
`PHP Beautify is now optional.\n\nPlease run: npm install --save-dev @tangible/php-beautify\n`,
)
continue
}
Expand Down Expand Up @@ -170,10 +174,8 @@ Documentation: ${homepage}#format

console.error(e.message)
}) // Let other tasks complete
})
}),
)

if (hasPhp) process.exit() // Exit to stop PHP process
}

module.exports = format
14 changes: 10 additions & 4 deletions commands/help.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function help() {
const { version, homepage, description } = require('../package.json')
import path from 'path'
import fs from 'fs-extra'
import { fileURLToPath } from 'url'

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

export default async function help() {
const { version, homepage, description } = await fs.readJson(
path.join(__dirname, '../package.json')
)

console.log(`Tangible Roller ${version}

Expand All @@ -23,5 +31,3 @@ Commands:
Documentation: ${homepage}
`)
}

module.exports = help
2 changes: 1 addition & 1 deletion commands/init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Init command: Initialize a project with a new config file, if it doesn't exist yet
*/
module.exports = async function() {
export default async function init() {


}
16 changes: 8 additions & 8 deletions commands/install.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const path = require('path')
const fs = require('fs/promises')
const { Readable } = require('stream')
const { extract } = require('zip-lib')
const run = require('../utils/run')
const fileExists = require('../utils/fileExists')
const prompt = require('../utils/prompt')
import path from 'path'
import fs from 'fs/promises'
import { Readable } from 'stream'
import { extract } from 'zip-lib'
import run from '../utils/run.js'
import fileExists from '../utils/fileExists.js'
import prompt from '../utils/prompt.js'

/**
* Install dependencies from Git repository or zip file URL
*/
module.exports = async function installCommand({ config }) {
export default async function installCommand({ config }) {
const { cwd, install } = config

if (!install) {
Expand Down
6 changes: 2 additions & 4 deletions commands/lint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const format = require('./format')
import format from './format.js'

async function lint(props) {
export default async function lint(props) {
return await format({
...props,
lint: true,
})
}

module.exports = lint
4 changes: 2 additions & 2 deletions commands/list.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const glob = require('fast-glob')
import glob from 'fast-glob'

/**
* List command: Lists all modules in the project with a config file (tangible.config.js)
*/
module.exports = async function() {
export default async function list() {

const configFileName = 'tangible.config.js'
const files = await glob([`**/${configFileName}`, '!**/node_modules/**'], {
Expand Down
21 changes: 10 additions & 11 deletions commands/run.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/**
* Run command: Build a script with ESBuild and run
*/
const path = require('path')
const { existsSync, readFileSync } = require('node:fs')
const { execSync } = require('node:child_process')
// const fs = require('node:fs')
// const url = require('node:url')
const { join: joinPath, isAbsolute } = require('node:path')
const esbuild = require('esbuild')
const { nodeExternalsPlugin } = require('esbuild-node-externals')
// const bunOr = require('./bun-or')
const prompt = require('../utils/prompt')
import path, { join as joinPath, isAbsolute } from 'node:path'
import { existsSync, readFileSync } from 'node:fs'
import { execSync } from 'node:child_process'
// import fs from 'node:fs'
// import url from 'node:url'
import esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'
// import bunOr from './bun-or.js'
import prompt from '../utils/prompt.js'

/**
* Replace __dirname based on source file path
Expand Down Expand Up @@ -41,7 +40,7 @@ const dirnamePlugin = {
},
}

module.exports = async function runEsbuild(props = {}) {
export default async function runEsbuild(props = {}) {
const {
argv: _argv = process.argv,
projectPath = process.cwd(),
Expand Down
18 changes: 8 additions & 10 deletions commands/serve.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const path = require('path')
const http = require('http')
const { execSync } = require('child_process')
const getPort = require('../utils/getPort')
const handler = require('serve-handler')
import path from 'path'
import http from 'http'
import { execSync } from 'child_process'
import handler from 'serve-handler'
import getPort, { portNumbers } from '../utils/getPort.js'

async function serve({ config }) {
export default async function serve({ config }) {
const { rootDir, isDev } = config

const serveOptions = config.serve || {}
Expand Down Expand Up @@ -43,7 +43,7 @@ async function serve({ config }) {
* @see https://github.com/remy/nodemon/blob/main/doc/requireable.md
*/

const nodemon = require('nodemon')
const nodemon = (await import('nodemon')).default

nodemon({
script: scriptPath,
Expand Down Expand Up @@ -86,7 +86,7 @@ async function serve({ config }) {
)

const availablePort = await getPort({
port: getPort.portNumbers(port, port + 100),
port: portNumbers(port, port + 100),
})

if (serveOptions.port && parseInt(serveOptions.port) !== availablePort) {
Expand All @@ -109,5 +109,3 @@ async function serve({ config }) {
})
})
}

module.exports = serve
4 changes: 2 additions & 2 deletions commands/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const installCommand = require('./install')
import installCommand from './install.js'

module.exports = async function updateCommand({ config }) {
export default async function updateCommand({ config }) {
return await installCommand({
config: {
...config,
Expand Down
16 changes: 7 additions & 9 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const path = require('path')
const fs = require('fs')
const { readFile } = require('fs/promises')
const url = require('url')
import path from 'path'
import fs from 'fs'
import { readFile } from 'fs/promises'
import url from 'url'

const prompt = require('../utils/prompt')
const run = require('../utils/run')
import prompt from '../utils/prompt.js'
import run from '../utils/run.js'

async function createConfig({ commandName, subproject }) {
export default async function createConfig({ commandName, subproject }) {
const cwd = process.cwd()
let rootDir = cwd
let isChildProjectFolder = false
Expand Down Expand Up @@ -192,5 +192,3 @@ async function createConfig({ commandName, subproject }) {
install,
}
}

module.exports = createConfig
Loading