Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: convert scripts to TS (#6160)
- Loading branch information
1 parent
aab303f
commit 15b6f1b
Showing
20 changed files
with
194 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 10 additions & 9 deletions
19
packages/create-vite/updateVersions.js → packages/create-vite/updateVersions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.