Skip to content

Commit

Permalink
Convert to ESM
Browse files Browse the repository at this point in the history
Signed-off-by: macdonst <[email protected]>
  • Loading branch information
macdonst committed May 15, 2024
1 parent 4861ce8 commit cffb14b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
9 changes: 7 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const arc = require('@architect/eslint-config')
import arc from '@architect/eslint-config'

module.exports = [
const config = [
...arc,
{
ignores: [
Expand All @@ -12,5 +12,10 @@ module.exports = [
'src/http/get-index',
'types/',
],
languageOptions: {
sourceType: 'module',
},
},
]

export default config
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.2",
"description": "TypeScript workflow integration for Enhance",
"main": "src/index.js",
"type": "module",
"scripts": {
"lint": "eslint . --fix",
"test": "npm run lint"
Expand Down
6 changes: 4 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { spawn } = require('child_process')
import { spawn } from 'child_process'

/**
* Spawns the TypeScript Compiler in watcher mode, for use during local development.
* Returns the process handle.
*/
exports.startWatcher = function startTypeScriptCompiler (inputPath, outputPath, options = {}) {
function startWatcher (inputPath, outputPath, options = {}) {
const { update } = options
const watcherProcess = spawn(
'npx',
Expand All @@ -22,3 +22,5 @@ exports.startWatcher = function startTypeScriptCompiler (inputPath, outputPath,
})
return watcherProcess
}

export { startWatcher }
65 changes: 33 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
const { updater } = require('@architect/utils')
const { join } = require('path')
const tsc = require('./cli')
const utils = require('./utils')
import { updater } from '@architect/utils'
import { join } from 'path'
import { startWatcher } from './cli.js'
import { copyProject, copyFile } from './utils.js'

let watcherProcess
const update = updater('TypeScriptCompiler')

module.exports = {
deploy: {
start: async () => {
// copy initial project files
utils.copyProject()
},
const deploy = {
start: async () => {
// copy initial project files
copyProject()
},
sandbox: {
start: async () => {
// copy initial project files
utils.copyProject()
// start tsc watcher
update.start('Starting watcher...')
watcherProcess = await tsc.startWatcher()
update.done('Started watcher')
},
end: () => {
update.start('Stopping watcher...')
watcherProcess.kill()
update.done('Stopped watcher')
},
watcher: async function (params) {
let { filename, /* event, */ inventory } = params
let tsDir = join(inventory.inv._project.cwd, 'ts')
if (filename.startsWith(tsDir) && !filename.endsWith('.ts') && !filename.endsWith('.tsx') && !filename.endsWith('.mts')) {
utils.copyFile(filename)
update.done(`${filename} updated`)
}
},
}

const sandbox = {
start: async () => {
// copy initial project files
copyProject()
// start tsc watcher
update.start('Starting watcher...')
watcherProcess = await startWatcher()
update.done('Started watcher')
},
end: () => {
update.start('Stopping watcher...')
watcherProcess.kill()
update.done('Stopped watcher')
},
watcher: async function (params) {
let { filename, /* event, */ inventory } = params
let tsDir = join(inventory.inv._project.cwd, 'ts')
if (filename.startsWith(tsDir) && !filename.endsWith('.ts') && !filename.endsWith('.tsx') && !filename.endsWith('.mts')) {
copyFile(filename)
update.done(`${filename} updated`)
}
},
}

export { deploy, sandbox }
15 changes: 7 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const { copyFileSync, mkdirSync, statSync } = require('fs')
const { globSync } = require('glob')
import { copyFileSync, mkdirSync, statSync } from 'fs'
import { globSync } from 'glob'

const SRC_DIR = 'ts'
const DEST_DIR = 'app'

function copyAllFiles () {
const files = [ 'ts', ...globSync('ts/**/*', { 'ignore': [ 'ts/**/*.mts', 'ts/**/*.ts', 'ts/**/*.tsx' ] }) ]
function copyProject () {
const files = globSync('ts/**/*', { 'ignore': [ 'ts/**/*.mts', 'ts/**/*.ts', 'ts/**/*.tsx' ] })
files.forEach((srcPath) => {
copyOneFile(srcPath)
copyFile(srcPath)
})
}

function copyOneFile (srcPath) {
function copyFile (srcPath) {
const destPath = srcPath.replace(SRC_DIR, DEST_DIR)
const stats = statSync(srcPath)
if (stats.isDirectory()) {
Expand All @@ -22,5 +22,4 @@ function copyOneFile (srcPath) {
}
}

exports.copyProject = copyAllFiles
exports.copyFile = copyOneFile
export { copyProject, copyFile }

0 comments on commit cffb14b

Please sign in to comment.