diff --git a/package.json b/package.json index dcb61a67..c4d6a04d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { "*": [ diff --git a/packages/plugin-vue-jsx/package.json b/packages/plugin-vue-jsx/package.json index 40ef71da..b55f44b7 100644 --- a/packages/plugin-vue-jsx/package.json +++ b/packages/plugin-vue-jsx/package.json @@ -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" diff --git a/packages/plugin-vue/package.json b/packages/plugin-vue/package.json index a3721f40..8e76dc4c 100644 --- a/packages/plugin-vue/package.json +++ b/packages/plugin-vue/package.json @@ -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" diff --git a/scripts/patchFileDeps.cjs b/scripts/patchFileDeps.ts similarity index 61% rename from scripts/patchFileDeps.cjs rename to scripts/patchFileDeps.ts index dbb42caa..0e90bbe8 100644 --- a/scripts/patchFileDeps.cjs +++ b/scripts/patchFileDeps.ts @@ -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(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}`) @@ -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')) } diff --git a/scripts/release.cjs b/scripts/release.ts similarity index 67% rename from scripts/release.cjs rename to scripts/release.ts index d7f258bc..37cd2d9c 100644 --- a/scripts/release.cjs +++ b/scripts/release.ts @@ -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', @@ -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 +) => ExecaChildProcess + +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 { + 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', @@ -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', @@ -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"?` @@ -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?` @@ -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 { const publicArgs = [ 'publish', '--no-git-tag-version', diff --git a/scripts/verifyCommit.cjs b/scripts/verifyCommit.ts similarity index 83% rename from scripts/verifyCommit.cjs rename to scripts/verifyCommit.ts index fbbff407..437e723c 100644 --- a/scripts/verifyCommit.cjs +++ b/scripts/verifyCommit.ts @@ -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 =