Skip to content

Commit

Permalink
chore: convert scripts to TS (#6160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Dec 18, 2021
1 parent d61b590 commit 8653e84
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 89 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.0.3",
"@types/node": "^16.11.14",
"@types/prompts": "^2.0.14",
"@types/semver": "^7.3.9",
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
Expand Down Expand Up @@ -61,7 +62,7 @@
},
"gitHooks": {
"pre-commit": "lint-staged --concurrent false",
"commit-msg": "node scripts/verifyCommit.cjs"
"commit-msg": "ts-node scripts/verifyCommit.ts"
},
"lint-staged": {
"*": [
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"types": "index.d.ts",
"scripts": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-vue-jsx",
"release": "node ../../scripts/release.cjs --skipBuild"
"release": "ts-node ../../scripts/release.ts --skipBuild"
},
"engines": {
"node": ">=12.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@vue/compiler-sfc --external:vue/compiler-sfc --external:vite --outfile=dist/index.js",
"build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-vue",
"release": "node ../../scripts/release.cjs"
"release": "ts-node ../../scripts/release.ts"
},
"engines": {
"node": ">=12.0.0"
Expand Down
21 changes: 11 additions & 10 deletions scripts/patchFileDeps.cjs → scripts/patchFileDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
// This script is called from postinstall hooks in playground packages that
// uses the file: protocol, and copies the file: deps into node_modules.

const fs = require('fs-extra')
const path = require('path')
import { copySync, removeSync } from 'fs-extra'
import { join, resolve } from 'path'

const root = process.cwd()
const pkg = require(path.join(root, 'package.json'))
const pkg = require(join(root, 'package.json'))

let hasPatched
for (const [key, val] of Object.entries(pkg.dependencies)) {
let hasPatched: boolean = false
for (const [key, val] of Object.entries<string>(pkg.dependencies)) {
if (val.startsWith('file:')) {
hasPatched = true
const src = path.resolve(root, val.slice('file:'.length))
const dest = path.resolve(root, 'node_modules', key)
fs.removeSync(dest)
fs.copySync(src, dest, {
const src = resolve(root, val.slice('file:'.length))
const dest = resolve(root, 'node_modules', key)
removeSync(dest)
copySync(src, dest, {
dereference: true
})
console.log(`patched ${val}`)
Expand All @@ -26,5 +27,5 @@ if (hasPatched) {
// remove node_modules/.ignored as pnpm will think our patched files are
// installed by another package manager and move them into this directory.
// On further installs it will error out if this directory is not empty.
fs.removeSync(path.resolve(root, 'node_modules', '.ignored'))
removeSync(resolve(root, 'node_modules', '.ignored'))
}
112 changes: 39 additions & 73 deletions scripts/release.cjs → scripts/release.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
// @ts-check

/**
* modified from https://github.com/vuejs/vue-next/blob/master/scripts/release.js
*/
const execa = require('execa')
const path = require('path')
const fs = require('fs')
import chalk from 'chalk'
import type { ExecaChildProcess, Options as ExecaOptions } from 'execa'
import execa from 'execa'
import { readFileSync, writeFileSync } from 'fs'
import path from 'path'
import prompts from 'prompts'
import type { ReleaseType } from 'semver'
import semver from 'semver'

const args = require('minimist')(process.argv.slice(2))
const semver = require('semver')
const chalk = require('chalk')
const prompts = require('prompts')

const pkgDir = process.cwd()
const pkgPath = path.resolve(pkgDir, 'package.json')
/**
* @type {{ name: string, version: string }}
*/
const pkg = require(pkgPath)
const pkg: { name: string; version: string } = require(pkgPath)
const pkgName = pkg.name.replace(/^@vitejs\//, '')
const currentVersion = pkg.version
/**
* @type {boolean}
*/
const isDryRun = args.dry
/**
* @type {boolean}
*/
const skipBuild = args.skipBuild
const isDryRun: boolean = args.dry
const skipBuild: boolean = args.skipBuild

/**
* @type {import('semver').ReleaseType[]}
*/
const versionIncrements = [
const versionIncrements: ReleaseType[] = [
'patch',
'minor',
'major',
Expand All @@ -41,43 +30,33 @@ const versionIncrements = [
'prerelease'
]

/**
* @param {import('semver').ReleaseType} i
*/
const inc = (i) => semver.inc(currentVersion, i, 'beta')
const inc: (i: ReleaseType) => string = (i) =>
semver.inc(currentVersion, i, 'beta')

/**
* @param {string} bin
* @param {string[]} args
* @param {object} opts
*/
const run = (bin, args, opts = {}) =>
type RunFn = (
bin: string,
args: string[],
opts?: ExecaOptions<string>
) => ExecaChildProcess<string>

const run: RunFn = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })

/**
* @param {string} bin
* @param {string[]} args
* @param {object} opts
*/
const dryRun = (bin, args, opts = {}) =>
type DryRunFn = (bin: string, args: string[], opts?: any) => void

const dryRun: DryRunFn = (bin, args, opts: any) =>
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)

const runIfNotDry = isDryRun ? dryRun : run

/**
* @param {string} msg
*/
const step = (msg) => console.log(chalk.cyan(msg))
const step: (msg: string) => void = (msg) => console.log(chalk.cyan(msg))

async function main() {
let targetVersion = args._[0]
async function main(): Promise<void> {
let targetVersion: string | undefined = args._[0]

if (!targetVersion) {
// no explicit version, offer suggestions
/**
* @type {{ release: string }}
*/
const { release } = await prompts({
const { release }: { release: string } = await prompts({
type: 'select',
name: 'release',
message: 'Select release type',
Expand All @@ -88,10 +67,7 @@ async function main() {
})

if (release === 'custom') {
/**
* @type {{ version: string }}
*/
const res = await prompts({
const res: { version: string } = await prompts({
type: 'text',
name: 'version',
message: 'Input custom version',
Expand All @@ -111,10 +87,7 @@ async function main() {
pkgName === 'vite' ? `v${targetVersion}` : `${pkgName}@${targetVersion}`

if (targetVersion.includes('beta') && !args.tag) {
/**
* @type {{ tagBeta: boolean }}
*/
const { tagBeta } = await prompts({
const { tagBeta }: { tagBeta: boolean } = await prompts({
type: 'confirm',
name: 'tagBeta',
message: `Publish under dist-tag "beta"?`
Expand All @@ -123,10 +96,7 @@ async function main() {
if (tagBeta) args.tag = 'beta'
}

/**
* @type {{ yes: boolean }}
*/
const { yes } = await prompts({
const { yes }: { yes: boolean } = await prompts({
type: 'confirm',
name: 'yes',
message: `Releasing ${tag}. Confirm?`
Expand Down Expand Up @@ -173,20 +143,16 @@ async function main() {
console.log()
}

/**
* @param {string} version
*/
function updateVersion(version) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
function updateVersion(version: string): void {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
pkg.version = version
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}

/**
* @param {string} version
* @param {Function} runIfNotDry
*/
async function publishPackage(version, runIfNotDry) {
async function publishPackage(
version: string,
runIfNotDry: RunFn | DryRunFn
): Promise<void> {
const publicArgs = [
'publish',
'--no-git-tag-version',
Expand Down
8 changes: 5 additions & 3 deletions scripts/verifyCommit.cjs → scripts/verifyCommit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Invoked on the commit-msg git hook by yorkie.

const chalk = require('chalk')
const msgPath = process.env.GIT_PARAMS
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim()
import chalk from 'chalk'
import { readFileSync } from 'fs'

const msgPath = process.env.GIT_PARAMS!
const msg = readFileSync(msgPath, 'utf-8').trim()

const releaseRE = /^v\d/
const commitRE =
Expand Down

0 comments on commit 8653e84

Please sign in to comment.