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 aab303f commit 15b6f1b
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 189 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -100,7 +100,7 @@ To work around this, playground packages that uses the `file:` protocol should a
```jsonc
"scripts": {
//...
"postinstall": "node ../../../scripts/patchFileDeps.cjs"
"postinstall": "ts-node ../../../scripts/patchFileDeps.ts"
}
```

Expand Down
3 changes: 2 additions & 1 deletion package.json
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/create-vite/package.json
Expand Up @@ -14,7 +14,7 @@
"main": "index.js",
"scripts": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package create-vite",
"release": "node updateVersions && node ../../scripts/release.cjs --skipBuild"
"release": "ts-node updateVersions && ts-node ../../scripts/release.ts --skipBuild"
},
"engines": {
"node": ">=12.0.0"
Expand Down
@@ -1,22 +1,23 @@
const fs = require('fs')
const path = require('path')
import { readdirSync, writeFileSync } from 'fs'
import { join } from 'path'

const latestVersion = require('../vite/package.json').version
const isLatestPreRelease = /beta|alpha|rc/.test(latestVersion)

;(async () => {
const templates = fs
.readdirSync(__dirname)
.filter((d) => d.startsWith('template-'))
for (const t of templates) {
const pkgPath = path.join(__dirname, t, `package.json`)
const templates = readdirSync(__dirname).filter((dir) =>
dir.startsWith('template-')
)
for (const template of templates) {
const pkgPath = join(__dirname, template, `package.json`)
const pkg = require(pkgPath)
if (!isLatestPreRelease) {
pkg.devDependencies.vite = `^` + latestVersion
}
if (t.startsWith('template-vue')) {
if (template.startsWith('template-vue')) {
pkg.devDependencies['@vitejs/plugin-vue'] =
`^` + require('../plugin-vue/package.json').version
}
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
})()
2 changes: 1 addition & 1 deletion packages/playground/alias/package.json
Expand Up @@ -7,7 +7,7 @@
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview",
"postinstall": "node ../../../scripts/patchFileDeps.cjs"
"postinstall": "ts-node ../../../scripts/patchFileDeps.ts"
},
"dependencies": {
"aliased-module": "file:./dir/module",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/optimize-deps/package.json
Expand Up @@ -7,7 +7,7 @@
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview",
"postinstall": "node ../../../scripts/patchFileDeps.cjs"
"postinstall": "ts-node ../../../scripts/patchFileDeps.ts"
},
"dependencies": {
"axios": "^0.24.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/optimize-missing-deps/package.json
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"scripts": {
"dev": "node server",
"postinstall": "node ../../../scripts/patchFileDeps.cjs"
"postinstall": "ts-node ../../../scripts/patchFileDeps.ts"
},
"dependencies": {
"missing-dep": "file:./missing-dep",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/ssr-deps/package.json
Expand Up @@ -6,7 +6,7 @@
"dev": "node server",
"serve": "cross-env NODE_ENV=production node server",
"debug": "node --inspect-brk server",
"postinstall": "node ../../../scripts/patchFileDeps.cjs"
"postinstall": "ts-node ../../../scripts/patchFileDeps.ts"
},
"dependencies": {
"bcrypt": "^5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-legacy/package.json
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-legacy",
"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-react/package.json
Expand Up @@ -18,7 +18,7 @@
"build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@babel/* --external:@rollup/* --external:resolve --external:react-refresh/* --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-react",
"release": "node ../../scripts/release.cjs"
"release": "ts-node ../../scripts/release.ts"
},
"engines": {
"node": ">=12.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue-jsx/package.json
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
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
52 changes: 41 additions & 11 deletions packages/vite/LICENSE.md
Expand Up @@ -1288,26 +1288,27 @@ Repository: https://github.com/mathiasbynens/cssesc.git

## debug
License: MIT
By: TJ Holowaychuk, Nathan Rajlich, Andrew Rhyne, Josh Junon
Repository: git://github.com/visionmedia/debug.git
By: Josh Junon, TJ Holowaychuk, Nathan Rajlich, Andrew Rhyne
Repository: git://github.com/debug-js/debug.git

> (The MIT License)
>
> Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca>
> Copyright (c) 2014-2017 TJ Holowaychuk <tj@vision-media.ca>
> Copyright (c) 2018-2021 Josh Junon
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software
> and associated documentation files (the 'Software'), to deal in the Software without restriction,
> including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software
> and associated documentation files (the 'Software'), to deal in the Software without restriction,
> including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
> and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
> subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial
> The above copyright notice and this permission notice shall be included in all copies or substantial
> portions of the Software.
>
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
> LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
> WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
> LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
> WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---------------------------------------
Expand Down Expand Up @@ -2887,6 +2888,35 @@ Repository: git://github.com/isaacs/minimatch.git
---------------------------------------

## mrmime
License: MIT
By: Luke Edwards
Repository: lukeed/mrmime

> The MIT License (MIT)
>
> Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (https://lukeed.com)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
---------------------------------------

## ms
License: MIT
Repository: zeit/ms
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/package.json
Expand Up @@ -35,12 +35,12 @@
"build-types": "run-s build-temp-types patch-types roll-types",
"build-temp-types": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
"ci-build": "rimraf dist && run-s build-bundle build-types",
"patch-types": "node scripts/patchTypes.cjs",
"patch-types": "ts-node scripts/patchTypes.ts",
"roll-types": "api-extractor run && rimraf temp",
"lint": "eslint --ext .ts src/**",
"format": "prettier --write --parser typescript \"src/**/*.ts\"",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path .",
"release": "node ../../scripts/release.cjs"
"release": "ts-node ../../scripts/release.ts"
},
"//": "READ CONTRIBUTING.md to understand what to put under deps vs. devDeps!",
"dependencies": {
Expand Down
70 changes: 0 additions & 70 deletions packages/vite/scripts/patchTypes.cjs

This file was deleted.

66 changes: 66 additions & 0 deletions packages/vite/scripts/patchTypes.ts
@@ -0,0 +1,66 @@
import type { ParseResult } from '@babel/parser'
import { parse } from '@babel/parser'
import type { File } from '@babel/types'
import chalk from 'chalk'
import { readdirSync, readFileSync, statSync, writeFileSync } from 'fs'
import MagicString from 'magic-string'
import { dirname, relative, resolve } from 'path'

const tempDir = resolve(__dirname, '../temp/node')
const typesDir = resolve(__dirname, '../types')

// walk through the temp dts dir, find all import/export of types/*
// and rewrite them into relative imports - so that api-extractor actually
// includes them in the rolled-up final d.ts file.
walkDir(tempDir)
console.log(chalk.green.bold(`patched types/* imports`))

function slash(p: string): string {
return p.replace(/\\/g, '/')
}

function walkDir(dir: string): void {
const files = readdirSync(dir)
for (const file of files) {
const resolved = resolve(dir, file)
const isDir = statSync(resolved).isDirectory()
if (isDir) {
walkDir(resolved)
} else {
rewriteFile(resolved)
}
}
}

function rewriteFile(file: string): void {
const content = readFileSync(file, 'utf-8')
const str = new MagicString(content)
let ast: ParseResult<File>
try {
ast = parse(content, {
sourceType: 'module',
plugins: ['typescript', 'classProperties']
})
} catch (e) {
console.log(chalk.red(`failed to parse ${file}`))
throw e
}
for (const statement of ast.program.body) {
if (
(statement.type === 'ImportDeclaration' ||
statement.type === 'ExportNamedDeclaration' ||
statement.type === 'ExportAllDeclaration') &&
statement.source?.value.startsWith('types/')
) {
const source = statement.source
const absoluteTypePath = resolve(typesDir, source.value.slice(6))
const relativeTypePath = slash(relative(dirname(file), absoluteTypePath))
str.overwrite(
source.start!,
source.end!,
JSON.stringify(relativeTypePath)
)
}
}
writeFileSync(file, str.toString())
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 15b6f1b

Please sign in to comment.