Skip to content

Commit

Permalink
refactor: put rss scripts into root package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed Nov 8, 2022
1 parent b59ff69 commit 4f3e589
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
"setup:husky": "husky install .husky",
"prepare": "is-ci || run-p setup:husky build:dev"
},
"rss": {
"clean:turbo": "rimraf .turbo",
"clean:build": "rimraf dist es lib",
"clean:coverage": "rimraf ./test/coverage",
"clean": "run-p clean:**",
"build:less": "rss",
"build:esm": "tsc --module esnext --target es6 --outDir ./es",
"build:cjs": "tsc --module commonjs --target es6 --outDir ./lib",
"build:umd": "rss-less && rollup -c ../../rollup.config.js --bundleConfigAsCjs",
"build:dev": "rss-less && run-p -s build:cjs build:esm",
"build:watch": "run-s -s build:watch:esm",
"build:watch:esm": "run-s -s 'build:esm -w'",
"build:watch:cjs": "run-s -s 'build:cjs -w'",
"build": "run-p -s build:dev build:umd",
"prebuild": "run-s -s clean:build",
"test": "rss-test",
"coveralls": "cat ./test/coverage/lcov.info | coveralls",
"pretest": "run-s -s clean:coverage"
},
"lint-staged": {
"*": [
"prettier --write --ignore-unknown"
Expand Down
27 changes: 14 additions & 13 deletions scripts/x6-build-tools/bin/rss.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
#!/usr/bin/env node

import _ from 'lodash'
import fs from 'fs'
import os from 'os'
import fse from 'fs-extra'
import path from 'path'
import crypto from 'crypto'
import colors from 'colors/safe.js'
import spawn from 'cross-spawn'
import { closest } from 'fastest-levenshtein'
import { createRequire } from 'module'
import { findMonorepoRootSync } from 'find-monorepo-root/sync'

const require = createRequire(import.meta.url)
const scripts = require('../lib/scripts.json')

const isWin32 = process.platform === 'win32'
let script
const event = process.env.npm_lifecycle_event
const script = scripts[event]
if (event != null) {
const { dir } = findMonorepoRootSync(process.cwd())
const { rss } = fse.readJSONSync(path.join(dir, 'package.json'))
script = rss[event]
}

if (script) {
const tmpdir = os.tmpdir()
const dir = path.join(tmpdir, 'rss')
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
if (!fse.existsSync(dir)) {
fse.mkdirSync(dir)
}
const hash = crypto.randomBytes(16).toString('hex')
const name = event.toLowerCase().replace(/[^0-9a-z]/g, '-')
const isWin32 = process.platform === 'win32'
const ext = isWin32 ? '.cmd' : ''
const file = path.join(dir, `${name}-${hash}`, ext)
const define = isWin32 ? '@ECHO OFF' : '#!/usr/bin/env sh'
const args = process.argv.slice(2)
const main = args.length ? `${script} ${args.join(' ')}` : script

fs.writeFileSync(file, `${define}\n\n${main}`)
fs.chmodSync(file, 0o777)
fse.writeFileSync(file, `${define}\n\n${main}`)
fse.chmodSync(file, 0o777)

const eventColor = closest(event, [
'green',
Expand All @@ -47,7 +48,7 @@ if (script) {
const child = spawn(file, { stdio: 'inherit' })

child.on('exit', () => {
fs.rmSync(file)
fse.rmSync(file)
})
} else {
console.error(`unknown script: [${event}]`)
Expand Down
5 changes: 2 additions & 3 deletions scripts/x6-build-tools/bin/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs'
import path from 'path'
import spawn from 'cross-spawn'
import colors from 'colors/safe.js'

const cwd = process.cwd()
const karmaConf = path.join(cwd, 'karma.conf.js')
Expand All @@ -18,8 +17,8 @@ if (npmPathIsJs) {
const cmd = fs.existsSync(karmaConf)
? ['karma', 'start']
: fs.existsSync(jestConf)
? ['jest']
: null
? ['jest']
: null

if (cmd) {
spawn(execPath, [...spawnArgs, ...cmd], { stdio: 'inherit' })
Expand Down
21 changes: 0 additions & 21 deletions scripts/x6-build-tools/lib/scripts.json

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/x6-build-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"colors": "^1.4.0",
"cross-spawn": "^7.0.3",
"fastest-levenshtein": "^1.0.16",
"fs-extra": "^10.0.0",
"lodash": "^4.17.21"
"find-monorepo-root": "^1.0.1",
"fs-extra": "^10.0.0"
},
"author": {
"name": "bubkoo",
Expand Down

0 comments on commit 4f3e589

Please sign in to comment.