Add rondo syncEsm command
This commit is contained in:
parent
92c93db822
commit
f817c75d57
@ -1,6 +1,7 @@
|
|||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import {argparse, arg} from '@rondo.dev/argparse'
|
import {argparse, arg} from '@rondo.dev/argparse'
|
||||||
import {join} from 'path'
|
import {join} from 'path'
|
||||||
|
import {info} from '../log'
|
||||||
|
|
||||||
export async function exportDir(...argv: string[]) {
|
export async function exportDir(...argv: string[]) {
|
||||||
const args = argparse({
|
const args = argparse({
|
||||||
@ -22,7 +23,6 @@ export async function exportDir(...argv: string[]) {
|
|||||||
.map(item => `export * from './${item}'\n`)
|
.map(item => `export * from './${item}'\n`)
|
||||||
.reduce((str, item) => str += item, '')
|
.reduce((str, item) => str += item, '')
|
||||||
|
|
||||||
// tslint:disable-next-line
|
info('Writing to %s', out)
|
||||||
console.log('Writing to %s', out)
|
|
||||||
fs.writeFileSync(join(dir, 'index.ts'), index)
|
fs.writeFileSync(join(dir, 'index.ts'), index)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,3 +2,4 @@ export * from './add'
|
|||||||
export * from './build'
|
export * from './build'
|
||||||
export * from './exportDir'
|
export * from './exportDir'
|
||||||
export * from './intergen'
|
export * from './intergen'
|
||||||
|
export * from './syncEsm'
|
||||||
|
|||||||
59
packages/scripts/src/scripts/syncEsm.ts
Normal file
59
packages/scripts/src/scripts/syncEsm.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import * as fs from 'fs'
|
||||||
|
import * as path from 'path'
|
||||||
|
import {argparse, arg} from '@rondo.dev/argparse'
|
||||||
|
import {info} from '../log'
|
||||||
|
|
||||||
|
const TSCONFIG_FILENAME = 'tsconfig.json'
|
||||||
|
const TSCONFIG_ESM_FILENAME = 'tsconfig.esm.json'
|
||||||
|
const PKG_DIRNAME = 'packages'
|
||||||
|
|
||||||
|
interface IRef {
|
||||||
|
path: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function syncEsm(...argv: string[]) {
|
||||||
|
const args = argparse({
|
||||||
|
packages: arg('string', {default: 'packages/', positional: true}),
|
||||||
|
help: arg('boolean', {alias: 'h'}),
|
||||||
|
})
|
||||||
|
.parse(argv)
|
||||||
|
|
||||||
|
const pkgDir = args.packages
|
||||||
|
|
||||||
|
const projects = fs.readdirSync(pkgDir)
|
||||||
|
.filter(file => {
|
||||||
|
const stat = fs.lstatSync(path.join(pkgDir, file))
|
||||||
|
return stat.isDirectory()
|
||||||
|
})
|
||||||
|
.map(file => path.join(pkgDir, file, TSCONFIG_FILENAME))
|
||||||
|
.filter(file => fs.existsSync(file))
|
||||||
|
.forEach(file => {
|
||||||
|
const tsconfig = JSON.parse(fs.readFileSync(file, 'utf8'))
|
||||||
|
const references = ((tsconfig.references || []) as IRef[])
|
||||||
|
.map(ref => ({
|
||||||
|
...ref,
|
||||||
|
path: path.join(ref.path, TSCONFIG_ESM_FILENAME),
|
||||||
|
}))
|
||||||
|
|
||||||
|
const tsconfigEsm = {
|
||||||
|
extends: `./${TSCONFIG_FILENAME}`,
|
||||||
|
compilerOptions: {
|
||||||
|
outDir: 'esm',
|
||||||
|
},
|
||||||
|
references,
|
||||||
|
}
|
||||||
|
|
||||||
|
const dirname = path.dirname(file)
|
||||||
|
const esmFile = path.join(dirname, TSCONFIG_ESM_FILENAME)
|
||||||
|
info('Writing %s', esmFile)
|
||||||
|
fs.writeFileSync(esmFile, JSON.stringify(tsconfigEsm, null, ' '))
|
||||||
|
|
||||||
|
const pkgFile = path.join(dirname, 'package.json')
|
||||||
|
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf8'))
|
||||||
|
pkg.module = 'lib/index.js'
|
||||||
|
pkg.module = pkg.main ? pkg.main.replace(/^lib/, 'esm') : 'lib/index.js'
|
||||||
|
|
||||||
|
info('Writing %s', pkgFile)
|
||||||
|
fs.writeFileSync(pkgFile, JSON.stringify(pkg, null, ' '))
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -1,47 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
const fs = require('fs')
|
|
||||||
const path = require('path')
|
|
||||||
|
|
||||||
const TSCONFIG_FILENAME = 'tsconfig.json'
|
|
||||||
const TSCONFIG_ESM_FILENAME = 'tsconfig.esm.json'
|
|
||||||
const PKG_DIRNAME = 'packages'
|
|
||||||
|
|
||||||
const projectRoot = path.relative(
|
|
||||||
process.cwd(), path.resolve(__dirname, '..'))
|
|
||||||
const pkgDir = path.join(projectRoot, PKG_DIRNAME)
|
|
||||||
|
|
||||||
const projects = fs.readdirSync(pkgDir)
|
|
||||||
.filter(file => {
|
|
||||||
const stat = fs.lstatSync(path.join(pkgDir, file))
|
|
||||||
return stat.isDirectory()
|
|
||||||
})
|
|
||||||
.map(file => path.join(pkgDir, file, TSCONFIG_FILENAME))
|
|
||||||
.filter(file => fs.existsSync(file))
|
|
||||||
.forEach(file => {
|
|
||||||
const tsconfig = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
||||||
const references = (tsconfig.references || [])
|
|
||||||
.map(ref => ({
|
|
||||||
...ref,
|
|
||||||
path: path.join(ref.path, TSCONFIG_ESM_FILENAME),
|
|
||||||
}))
|
|
||||||
|
|
||||||
const tsconfigEsm = {
|
|
||||||
extends: `./${TSCONFIG_FILENAME}`,
|
|
||||||
compilerOptions: {
|
|
||||||
"outDir": "esm",
|
|
||||||
},
|
|
||||||
references,
|
|
||||||
}
|
|
||||||
|
|
||||||
const dirname = path.dirname(file)
|
|
||||||
const esmFile = path.join(dirname, TSCONFIG_ESM_FILENAME)
|
|
||||||
console.log('Writing %s', esmFile)
|
|
||||||
fs.writeFileSync(esmFile, JSON.stringify(tsconfigEsm, null, ' '))
|
|
||||||
|
|
||||||
const pkgFile = path.join(dirname, 'package.json')
|
|
||||||
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf8'))
|
|
||||||
pkg.module = 'lib/index.js'
|
|
||||||
pkg.module = pkg.main ? pkg.main.replace(/^lib/, 'esm') : 'lib/index.js'
|
|
||||||
console.log('Writing %s', pkgFile)
|
|
||||||
fs.writeFileSync(pkgFile, JSON.stringify(pkg, null, ' '))
|
|
||||||
})
|
|
||||||
Loading…
x
Reference in New Issue
Block a user