Skip to content

Commit 8653e84

Browse files
authored
chore: convert scripts to TS (#6160)
1 parent d61b590 commit 8653e84

File tree

6 files changed

+59
-89
lines changed

6 files changed

+59
-89
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@types/fs-extra": "^9.0.13",
2929
"@types/jest": "^27.0.3",
3030
"@types/node": "^16.11.14",
31+
"@types/prompts": "^2.0.14",
3132
"@types/semver": "^7.3.9",
3233
"@typescript-eslint/eslint-plugin": "^5.7.0",
3334
"@typescript-eslint/parser": "^5.7.0",
@@ -61,7 +62,7 @@
6162
},
6263
"gitHooks": {
6364
"pre-commit": "lint-staged --concurrent false",
64-
"commit-msg": "node scripts/verifyCommit.cjs"
65+
"commit-msg": "ts-node scripts/verifyCommit.ts"
6566
},
6667
"lint-staged": {
6768
"*": [

packages/plugin-vue-jsx/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"types": "index.d.ts",
1212
"scripts": {
1313
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-vue-jsx",
14-
"release": "node ../../scripts/release.cjs --skipBuild"
14+
"release": "ts-node ../../scripts/release.ts --skipBuild"
1515
},
1616
"engines": {
1717
"node": ">=12.0.0"

packages/plugin-vue/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"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",
1717
"build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp",
1818
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-vue",
19-
"release": "node ../../scripts/release.cjs"
19+
"release": "ts-node ../../scripts/release.ts"
2020
},
2121
"engines": {
2222
"node": ">=12.0.0"

scripts/patchFileDeps.cjs scripts/patchFileDeps.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
// This script is called from postinstall hooks in playground packages that
44
// uses the file: protocol, and copies the file: deps into node_modules.
55

6-
const fs = require('fs-extra')
7-
const path = require('path')
6+
import { copySync, removeSync } from 'fs-extra'
7+
import { join, resolve } from 'path'
8+
89
const root = process.cwd()
9-
const pkg = require(path.join(root, 'package.json'))
10+
const pkg = require(join(root, 'package.json'))
1011

11-
let hasPatched
12-
for (const [key, val] of Object.entries(pkg.dependencies)) {
12+
let hasPatched: boolean = false
13+
for (const [key, val] of Object.entries<string>(pkg.dependencies)) {
1314
if (val.startsWith('file:')) {
1415
hasPatched = true
15-
const src = path.resolve(root, val.slice('file:'.length))
16-
const dest = path.resolve(root, 'node_modules', key)
17-
fs.removeSync(dest)
18-
fs.copySync(src, dest, {
16+
const src = resolve(root, val.slice('file:'.length))
17+
const dest = resolve(root, 'node_modules', key)
18+
removeSync(dest)
19+
copySync(src, dest, {
1920
dereference: true
2021
})
2122
console.log(`patched ${val}`)
@@ -26,5 +27,5 @@ if (hasPatched) {
2627
// remove node_modules/.ignored as pnpm will think our patched files are
2728
// installed by another package manager and move them into this directory.
2829
// On further installs it will error out if this directory is not empty.
29-
fs.removeSync(path.resolve(root, 'node_modules', '.ignored'))
30+
removeSync(resolve(root, 'node_modules', '.ignored'))
3031
}

scripts/release.cjs scripts/release.ts

+39-73
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
1-
// @ts-check
2-
31
/**
42
* modified from https://github.com/vuejs/vue-next/blob/master/scripts/release.js
53
*/
6-
const execa = require('execa')
7-
const path = require('path')
8-
const fs = require('fs')
4+
import chalk from 'chalk'
5+
import type { ExecaChildProcess, Options as ExecaOptions } from 'execa'
6+
import execa from 'execa'
7+
import { readFileSync, writeFileSync } from 'fs'
8+
import path from 'path'
9+
import prompts from 'prompts'
10+
import type { ReleaseType } from 'semver'
11+
import semver from 'semver'
12+
913
const args = require('minimist')(process.argv.slice(2))
10-
const semver = require('semver')
11-
const chalk = require('chalk')
12-
const prompts = require('prompts')
1314

1415
const pkgDir = process.cwd()
1516
const pkgPath = path.resolve(pkgDir, 'package.json')
16-
/**
17-
* @type {{ name: string, version: string }}
18-
*/
19-
const pkg = require(pkgPath)
17+
const pkg: { name: string; version: string } = require(pkgPath)
2018
const pkgName = pkg.name.replace(/^@vitejs\//, '')
2119
const currentVersion = pkg.version
22-
/**
23-
* @type {boolean}
24-
*/
25-
const isDryRun = args.dry
26-
/**
27-
* @type {boolean}
28-
*/
29-
const skipBuild = args.skipBuild
20+
const isDryRun: boolean = args.dry
21+
const skipBuild: boolean = args.skipBuild
3022

31-
/**
32-
* @type {import('semver').ReleaseType[]}
33-
*/
34-
const versionIncrements = [
23+
const versionIncrements: ReleaseType[] = [
3524
'patch',
3625
'minor',
3726
'major',
@@ -41,43 +30,33 @@ const versionIncrements = [
4130
'prerelease'
4231
]
4332

44-
/**
45-
* @param {import('semver').ReleaseType} i
46-
*/
47-
const inc = (i) => semver.inc(currentVersion, i, 'beta')
33+
const inc: (i: ReleaseType) => string = (i) =>
34+
semver.inc(currentVersion, i, 'beta')
4835

49-
/**
50-
* @param {string} bin
51-
* @param {string[]} args
52-
* @param {object} opts
53-
*/
54-
const run = (bin, args, opts = {}) =>
36+
type RunFn = (
37+
bin: string,
38+
args: string[],
39+
opts?: ExecaOptions<string>
40+
) => ExecaChildProcess<string>
41+
42+
const run: RunFn = (bin, args, opts = {}) =>
5543
execa(bin, args, { stdio: 'inherit', ...opts })
5644

57-
/**
58-
* @param {string} bin
59-
* @param {string[]} args
60-
* @param {object} opts
61-
*/
62-
const dryRun = (bin, args, opts = {}) =>
45+
type DryRunFn = (bin: string, args: string[], opts?: any) => void
46+
47+
const dryRun: DryRunFn = (bin, args, opts: any) =>
6348
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
6449

6550
const runIfNotDry = isDryRun ? dryRun : run
6651

67-
/**
68-
* @param {string} msg
69-
*/
70-
const step = (msg) => console.log(chalk.cyan(msg))
52+
const step: (msg: string) => void = (msg) => console.log(chalk.cyan(msg))
7153

72-
async function main() {
73-
let targetVersion = args._[0]
54+
async function main(): Promise<void> {
55+
let targetVersion: string | undefined = args._[0]
7456

7557
if (!targetVersion) {
7658
// no explicit version, offer suggestions
77-
/**
78-
* @type {{ release: string }}
79-
*/
80-
const { release } = await prompts({
59+
const { release }: { release: string } = await prompts({
8160
type: 'select',
8261
name: 'release',
8362
message: 'Select release type',
@@ -88,10 +67,7 @@ async function main() {
8867
})
8968

9069
if (release === 'custom') {
91-
/**
92-
* @type {{ version: string }}
93-
*/
94-
const res = await prompts({
70+
const res: { version: string } = await prompts({
9571
type: 'text',
9672
name: 'version',
9773
message: 'Input custom version',
@@ -111,10 +87,7 @@ async function main() {
11187
pkgName === 'vite' ? `v${targetVersion}` : `${pkgName}@${targetVersion}`
11288

11389
if (targetVersion.includes('beta') && !args.tag) {
114-
/**
115-
* @type {{ tagBeta: boolean }}
116-
*/
117-
const { tagBeta } = await prompts({
90+
const { tagBeta }: { tagBeta: boolean } = await prompts({
11891
type: 'confirm',
11992
name: 'tagBeta',
12093
message: `Publish under dist-tag "beta"?`
@@ -123,10 +96,7 @@ async function main() {
12396
if (tagBeta) args.tag = 'beta'
12497
}
12598

126-
/**
127-
* @type {{ yes: boolean }}
128-
*/
129-
const { yes } = await prompts({
99+
const { yes }: { yes: boolean } = await prompts({
130100
type: 'confirm',
131101
name: 'yes',
132102
message: `Releasing ${tag}. Confirm?`
@@ -173,20 +143,16 @@ async function main() {
173143
console.log()
174144
}
175145

176-
/**
177-
* @param {string} version
178-
*/
179-
function updateVersion(version) {
180-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
146+
function updateVersion(version: string): void {
147+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
181148
pkg.version = version
182-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
149+
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
183150
}
184151

185-
/**
186-
* @param {string} version
187-
* @param {Function} runIfNotDry
188-
*/
189-
async function publishPackage(version, runIfNotDry) {
152+
async function publishPackage(
153+
version: string,
154+
runIfNotDry: RunFn | DryRunFn
155+
): Promise<void> {
190156
const publicArgs = [
191157
'publish',
192158
'--no-git-tag-version',

scripts/verifyCommit.cjs scripts/verifyCommit.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Invoked on the commit-msg git hook by yorkie.
22

3-
const chalk = require('chalk')
4-
const msgPath = process.env.GIT_PARAMS
5-
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim()
3+
import chalk from 'chalk'
4+
import { readFileSync } from 'fs'
5+
6+
const msgPath = process.env.GIT_PARAMS!
7+
const msg = readFileSync(msgPath, 'utf-8').trim()
68

79
const releaseRE = /^v\d/
810
const commitRE =

0 commit comments

Comments
 (0)