Skip to content

Commit 5b63a75

Browse files
committed
use node built-in fs
1 parent b8f876d commit 5b63a75

File tree

6 files changed

+51
-48
lines changed

6 files changed

+51
-48
lines changed

packages/core/scripts/build/generateServiceClient.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/* eslint-disable no-restricted-imports */
77
import * as proc from 'child_process'
8-
import * as fs from 'fs-extra'
8+
import * as nodefs from 'fs'
99
import * as path from 'path'
1010

1111
const repoRoot = path.join(process.cwd(), '../../') // root/packages/toolkit -> root/
@@ -34,7 +34,7 @@ async function generateServiceClients(serviceClientDefinitions: ServiceClientDef
3434

3535
/** When cloning aws-sdk-js, we want to pull the version actually used in package-lock.json. */
3636
function getJsSdkVersion(): string {
37-
const json = fs.readFileSync(path.resolve(repoRoot, 'package-lock.json')).toString()
37+
const json = nodefs.readFileSync(path.resolve(repoRoot, 'package-lock.json')).toString()
3838
const packageLock = JSON.parse(json)
3939

4040
return packageLock['packages']['node_modules/aws-sdk']['version']
@@ -116,7 +116,7 @@ async function insertServiceClientsIntoJsSdk(
116116
'apis',
117117
`${serviceClientDefinition.serviceName.toLowerCase()}-${apiVersion}.normal.json`
118118
)
119-
fs.copyFileSync(serviceClientDefinition.serviceJsonPath, jsSdkServiceJsonPath)
119+
nodefs.copyFileSync(serviceClientDefinition.serviceJsonPath, jsSdkServiceJsonPath)
120120
})
121121

122122
const apiMetadataPath = path.join(jsSdkPath, 'apis', 'metadata.json')
@@ -133,7 +133,7 @@ interface ServiceJsonSchema {
133133
}
134134

135135
function getApiVersion(serviceJsonPath: string): string {
136-
const json = fs.readFileSync(serviceJsonPath).toString()
136+
const json = nodefs.readFileSync(serviceJsonPath).toString()
137137
const serviceJson = JSON.parse(json) as ServiceJsonSchema
138138

139139
return serviceJson.metadata.apiVersion
@@ -149,14 +149,14 @@ interface ApiMetadata {
149149
async function patchServicesIntoApiMetadata(apiMetadataPath: string, serviceNames: string[]): Promise<void> {
150150
console.log(`Patching services (${serviceNames.join(', ')}) into API Metadata...`)
151151

152-
const apiMetadataJson = fs.readFileSync(apiMetadataPath).toString()
152+
const apiMetadataJson = nodefs.readFileSync(apiMetadataPath).toString()
153153
const apiMetadata = JSON.parse(apiMetadataJson) as ApiMetadata
154154

155155
serviceNames.forEach((serviceName) => {
156156
apiMetadata[serviceName.toLowerCase()] = { name: serviceName }
157157
})
158158

159-
fs.writeFileSync(apiMetadataPath, JSON.stringify(apiMetadata, undefined, 4))
159+
nodefs.writeFileSync(apiMetadataPath, JSON.stringify(apiMetadata, undefined, 4))
160160
}
161161

162162
/**
@@ -198,7 +198,7 @@ async function integrateServiceClient(repoPath: string, serviceJsonPath: string,
198198

199199
console.log(`Integrating ${typingsFilename} ...`)
200200

201-
fs.copyFileSync(sourceClientPath, destinationClientPath)
201+
nodefs.copyFileSync(sourceClientPath, destinationClientPath)
202202

203203
await sanitizeServiceClient(destinationClientPath)
204204
}
@@ -209,7 +209,7 @@ async function integrateServiceClient(repoPath: string, serviceJsonPath: string,
209209
async function sanitizeServiceClient(generatedClientPath: string): Promise<void> {
210210
console.log('Altering Service Client to fit the codebase...')
211211

212-
let fileContents = fs.readFileSync(generatedClientPath).toString()
212+
let fileContents = nodefs.readFileSync(generatedClientPath).toString()
213213

214214
// Add a header stating the file is autogenerated
215215
fileContents = `
@@ -223,7 +223,7 @@ ${fileContents}
223223

224224
fileContents = fileContents.replace(/(import .* from.*)\.\.(.*)/g, '$1aws-sdk$2')
225225

226-
fs.writeFileSync(generatedClientPath, fileContents)
226+
nodefs.writeFileSync(generatedClientPath, fileContents)
227227
}
228228

229229
// ---------------------------------------------------------------------------------------------------------------------

scripts/generateIcons.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
import webfont from 'webfont'
77
import * as path from 'path'
8-
import * as fs from 'fs-extra'
8+
import * as nodefs from 'fs'
99

1010
const fontId = 'aws-toolkit-icons'
1111
const projectDir = process.cwd() // root/packages/toolkit
1212
const rootDir = path.join(projectDir, '../..') // root/
1313
const iconsDir = path.join(projectDir, 'resources', 'icons')
1414
const fontsDir = path.join(projectDir, 'resources', 'fonts')
1515
const stylesheetsDir = path.join(projectDir, 'resources', 'css')
16-
const packageJson = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), { encoding: 'utf-8' }))
16+
const packageJson = JSON.parse(nodefs.readFileSync(path.join(projectDir, 'package.json'), { encoding: 'utf-8' }))
1717
const iconSources = [
1818
// Paths relative to packages/toolkit
1919
`resources/icons/**/*.svg`,
@@ -69,7 +69,7 @@ async function updatePackage(fontPath: string, icons: [id: string, icon: Package
6969

7070
// prettier adds a newline to JSON files
7171
const newPackage = `${JSON.stringify(packageJson, undefined, 4)}\n`
72-
await fs.writeFile(path.join(projectDir, 'package.json'), newPackage)
72+
nodefs.writeFileSync(path.join(projectDir, 'package.json'), newPackage)
7373
console.log('Updated package.json')
7474
}
7575

@@ -81,15 +81,15 @@ const themes = {
8181
async function generateCloud9Icons(targets: { name: string; path: string }[], destination: string): Promise<void> {
8282
console.log('Generating icons for Cloud9')
8383

84-
async function replaceColor(file: string, color: string, dst: string): Promise<void> {
85-
const contents = await fs.readFile(file, 'utf-8')
84+
function replaceColor(file: string, color: string, dst: string): void {
85+
const contents = nodefs.readFileSync(file, 'utf-8')
8686
const replaced = contents.replace(/currentColor/g, color)
87-
await fs.writeFile(dst, replaced)
87+
nodefs.writeFileSync(dst, replaced)
8888
}
8989

9090
for (const [theme, color] of Object.entries(themes)) {
9191
const themeDest = path.join(destination, theme)
92-
await fs.mkdirp(themeDest)
92+
nodefs.mkdirSync(themeDest, { recursive: true })
9393
await Promise.all(targets.map((t) => replaceColor(t.path, color, path.join(themeDest, `${t.name}.svg`))))
9494
}
9595
}
@@ -169,9 +169,11 @@ ${result.template}
169169
const cloud9Dest = path.join(iconsDir, 'cloud9', 'generated')
170170
const isValidIcon = (i: (typeof icons)[number]): i is Required<typeof i> => i.data !== undefined
171171

172-
await fs.mkdirp(fontsDir)
173-
await fs.writeFile(dest, result.woff)
174-
await fs.writeFile(stylesheetPath, template)
172+
nodefs.mkdirSync(fontsDir, { recursive: true })
173+
if (result.woff) {
174+
nodefs.writeFileSync(dest, result.woff)
175+
}
176+
nodefs.writeFileSync(stylesheetPath, template)
175177
await updatePackage(
176178
`./${relativeDest}`,
177179
icons.filter(isValidIcon).map((i) => [i.name, i.data])
@@ -182,7 +184,7 @@ ${result.template}
182184
generated.addEntry(stylesheetPath)
183185
generated.addEntry(cloud9Dest)
184186

185-
await generated.emit(path.join(projectDir, 'dist'))
187+
generated.emit(path.join(projectDir, 'dist'))
186188
}
187189

188190
class GeneratedFilesManifest {
@@ -192,17 +194,17 @@ class GeneratedFilesManifest {
192194
this.files.push(file)
193195
}
194196

195-
public async emit(dir: string): Promise<void> {
197+
public emit(dir: string): void {
196198
const dest = path.join(dir, 'generated.buildinfo')
197199
const data = JSON.stringify(this.files, undefined, 4)
198-
await fs.mkdirp(dir)
199-
await fs.writeFile(dest, data)
200+
nodefs.mkdirSync(dir, { recursive: true })
201+
nodefs.writeFileSync(dest, data)
200202
}
201203
}
202204

203205
async function loadCodiconMappings(): Promise<Record<string, number | undefined>> {
204206
const codicons = path.join(rootDir, 'node_modules', '@vscode', 'codicons', 'src')
205-
const data = JSON.parse(await fs.readFile(path.join(codicons, 'template', 'mapping.json'), 'utf-8'))
207+
const data = JSON.parse(nodefs.readFileSync(path.join(codicons, 'template', 'mapping.json'), 'utf-8'))
206208
const mappings: Record<string, number | undefined> = {}
207209
for (const [k, v] of Object.entries(data)) {
208210
if (typeof k === 'string' && typeof v === 'number') {

scripts/generateNonCodeFiles.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as child_process from 'child_process'
7-
import * as fs from 'fs-extra'
7+
import * as nodefs from 'fs'
88
import { marked } from 'marked'
99
import * as path from 'path'
1010

@@ -27,14 +27,14 @@ function translateReadmeToHtml(
2727
cn: boolean = false
2828
) {
2929
const inputPath = path.join(root, inputFile)
30-
if (!fs.existsSync(inputPath)) {
30+
if (!nodefs.existsSync(inputPath)) {
3131
if (throwIfNotExists) {
3232
throw Error(`File ${inputFile} was not found, but it is required.`)
3333
}
3434
console.log(`File ${inputFile} was not found, skipping transformation...`)
3535
return
3636
}
37-
const fileText = fs.readFileSync(path.join(root, inputFile)).toString()
37+
const fileText = nodefs.readFileSync(path.join(root, inputFile)).toString()
3838
const relativePathRegex = /]\(\.\//g
3939
let transformedText = fileText.replace(relativePathRegex, '](!!EXTENSIONROOT!!/')
4040
if (cn) {
@@ -45,7 +45,7 @@ function translateReadmeToHtml(
4545
if (typeof r !== 'string') {
4646
throw Error()
4747
}
48-
fs.writeFileSync(path.join(root, outputFile), r)
48+
nodefs.writeFileSync(path.join(root, outputFile), r)
4949
}
5050

5151
/**
@@ -54,7 +54,8 @@ function translateReadmeToHtml(
5454
function generateFileHash(root: string) {
5555
try {
5656
const response = child_process.execSync('git rev-parse HEAD')
57-
fs.outputFileSync(path.join(root, '.gitcommit'), response)
57+
nodefs.mkdirSync(root, { recursive: true })
58+
nodefs.writeFileSync(path.join(root, '.gitcommit'), response)
5859
} catch (e) {
5960
console.log(`Getting commit hash failed ${e}`)
6061
}

scripts/generateSettings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import * as fs from 'fs-extra'
6+
import * as nodefs from 'fs'
77
import * as path from 'path'
88

99
/**
@@ -17,7 +17,7 @@ import * as path from 'path'
1717
function main() {
1818
const ext = path.basename(process.cwd())
1919
const packageJsonFile = './package.json'
20-
const packageJson = JSON.parse(fs.readFileSync(packageJsonFile, { encoding: 'utf-8' }))
20+
const packageJson = JSON.parse(nodefs.readFileSync(packageJsonFile, { encoding: 'utf-8' }))
2121

2222
const genFile = `../core/src/shared/settings-${ext}.gen.ts`
2323
type Configuration = { [key: string]: { [key: string]: {} } }
@@ -50,7 +50,7 @@ export const ${ext}Settings = ${JSON.stringify(settings, undefined, ' ')}
5050
export default ${ext}Settings
5151
`
5252

53-
fs.writeFileSync(genFile, contents)
53+
nodefs.writeFileSync(genFile, contents)
5454
}
5555

5656
main()

scripts/newChange.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as child_process from 'child_process'
7-
import * as fs from 'fs-extra'
7+
import * as nodefs from 'fs'
88
import { join } from 'path'
99
import * as readlineSync from 'readline-sync'
1010
import * as crypto from 'crypto'
@@ -40,7 +40,7 @@ function promptForChange(): string {
4040
}
4141
}
4242

43-
fs.mkdirpSync(directory)
43+
nodefs.mkdirSync(directory, { recursive: true })
4444

4545
const type = promptForType()
4646
const description = promptForChange()
@@ -50,7 +50,7 @@ const contents: NewChange = {
5050
}
5151
const fileName = `${type}-${crypto.randomUUID()}.json`
5252
const path = join(directory, fileName)
53-
fs.writeFileSync(path, JSON.stringify(contents, undefined, '\t') + '\n')
53+
nodefs.writeFileSync(path, JSON.stringify(contents, undefined, '\t') + '\n')
5454

5555
console.log(`Change log written to ${path}`)
5656
child_process.execSync(`git add ${directory}`)

scripts/package.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//
1919

2020
import * as child_process from 'child_process'
21-
import * as fs from 'fs-extra'
21+
import * as nodefs from 'fs'
2222
import * as path from 'path'
2323

2424
function parseArgs() {
@@ -114,7 +114,7 @@ function main() {
114114
const webpackConfigJsFile = '../webpack.base.config.js'
115115
const backupWebpackConfigFile = `${webpackConfigJsFile}.package.bk`
116116

117-
if (!fs.existsSync(packageJsonFile)) {
117+
if (!nodefs.existsSync(packageJsonFile)) {
118118
throw new Error(`package.json not found, cannot package this directory: ${process.cwd()}`)
119119
}
120120

@@ -127,8 +127,8 @@ function main() {
127127
throw new Error('Cannot package VSIX as both a release and a beta simultaneously')
128128
}
129129
// Create backup file so we can restore the originals later.
130-
fs.copyFileSync(packageJsonFile, backupJsonFile)
131-
const packageJson = JSON.parse(fs.readFileSync(packageJsonFile, { encoding: 'utf-8' }))
130+
nodefs.copyFileSync(packageJsonFile, backupJsonFile)
131+
const packageJson = JSON.parse(nodefs.readFileSync(packageJsonFile, { encoding: 'utf-8' }))
132132

133133
if (!release || args.debug) {
134134
const versionSuffix = getVersionSuffix(args.feature, args.debug)
@@ -148,13 +148,13 @@ function main() {
148148
}
149149

150150
if (args.debug) {
151-
fs.copyFileSync(webpackConfigJsFile, backupWebpackConfigFile)
152-
const webpackConfigJs = fs.readFileSync(webpackConfigJsFile, { encoding: 'utf-8' })
153-
fs.writeFileSync(webpackConfigJsFile, webpackConfigJs.replace(/minimize: true/, 'minimize: false'))
151+
nodefs.copyFileSync(webpackConfigJsFile, backupWebpackConfigFile)
152+
const webpackConfigJs = nodefs.readFileSync(webpackConfigJsFile, { encoding: 'utf-8' })
153+
nodefs.writeFileSync(webpackConfigJsFile, webpackConfigJs.replace(/minimize: true/, 'minimize: false'))
154154
}
155155
}
156156

157-
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, undefined, ' '))
157+
nodefs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, undefined, ' '))
158158
child_process.execFileSync(
159159
'vsce',
160160
[
@@ -181,17 +181,17 @@ function main() {
181181
// TODO: Once we can support releasing multiple artifacts,
182182
// let's just keep the .vsix in its respective project folder in packages/
183183
const vsixName = `${packageJson.name}-${packageJson.version}.vsix`
184-
fs.moveSync(vsixName, `../../${vsixName}`, { overwrite: true })
184+
nodefs.renameSync(vsixName, `../../${vsixName}`)
185185
} catch (e) {
186186
console.log(e)
187187
throw Error('package.ts: failed')
188188
} finally {
189189
// Restore the original files.
190-
fs.copyFileSync(backupJsonFile, packageJsonFile)
191-
fs.unlinkSync(backupJsonFile)
190+
nodefs.copyFileSync(backupJsonFile, packageJsonFile)
191+
nodefs.unlinkSync(backupJsonFile)
192192
if (args.debug) {
193-
fs.copyFileSync(backupWebpackConfigFile, webpackConfigJsFile)
194-
fs.unlinkSync(backupWebpackConfigFile)
193+
nodefs.copyFileSync(backupWebpackConfigFile, webpackConfigJsFile)
194+
nodefs.unlinkSync(backupWebpackConfigFile)
195195
}
196196
}
197197
}

0 commit comments

Comments
 (0)