From bd496f16206752df5a325325df52b55733708ff4 Mon Sep 17 00:00:00 2001 From: Sonny3788 Date: Sun, 11 Dec 2022 01:11:08 -0500 Subject: [PATCH 1/3] Revert "vscode files" This reverts commit f3a52ef3c1ae75681faf87f0dc094f6a544a33cc. --- .vscode/extensions.json | 7 ------- .vscode/settings.json | 5 +---- 2 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 .vscode/extensions.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index 9126e1c..0000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "recommendations": [ - "esbenp.prettier-vscode", - "visualstudioexptteam.vscodeintellicode", - "dbaeumer.vscode-eslint" - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index d250f95..1de1d1b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,8 +2,5 @@ "prettier.enable": true, "editor.formatOnSave": true, "editor.formatOnSaveMode": "modificationsIfAvailable", - "cSpell.enabled": true, - "cSpell.words": [ - "vsix" - ], + "cSpell.enabled": true } \ No newline at end of file From 8a28bdd4934411f0c0547f5d4a683e11645c9e78 Mon Sep 17 00:00:00 2001 From: Sonny3788 Date: Sun, 11 Dec 2022 01:52:14 -0500 Subject: [PATCH 2/3] friend --- .vscode/extensions.json | 0 .vscode/settings.json | 5 ++++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .vscode/extensions.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e69de29 diff --git a/.vscode/settings.json b/.vscode/settings.json index 1de1d1b..966407a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,8 @@ "prettier.enable": true, "editor.formatOnSave": true, "editor.formatOnSaveMode": "modificationsIfAvailable", - "cSpell.enabled": true + "cSpell.enabled": true, + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] } \ No newline at end of file From 015dd52d216a835c231d5556d4b9050c554b2873 Mon Sep 17 00:00:00 2001 From: Sonny3788 Date: Sun, 11 Dec 2022 02:39:51 -0500 Subject: [PATCH 3/3] configUtils --- .../VSCodeExtensionsPackageJsonGenerator.ts | 165 +++++++++++++++++ src/webpack/index.ts | 172 +----------------- 2 files changed, 168 insertions(+), 169 deletions(-) create mode 100644 src/webpack/VSCodeExtensionsPackageJsonGenerator.ts diff --git a/src/webpack/VSCodeExtensionsPackageJsonGenerator.ts b/src/webpack/VSCodeExtensionsPackageJsonGenerator.ts new file mode 100644 index 0000000..fbfe5ab --- /dev/null +++ b/src/webpack/VSCodeExtensionsPackageJsonGenerator.ts @@ -0,0 +1,165 @@ +import { Compiler } from 'webpack'; +import { createConfigurationObject } from '../vscode-extension-config'; +import * as fs from 'fs/promises'; +import { GeneratingConfiguration, PackageJson } from '../types'; +import { + InvalidConfigurationError, + readGeneratingConfiguration, + validateInputConfig, + withDefaultConfig +} from '../input-configuration-helper'; +import { WebpackLogger, PLUGIN } from './index'; + + +export class VSCodeExtensionsPackageJsonGenerator { + private readonly definitionsFile: string | undefined; + private definitions: Required | undefined; + private needsUpdate: boolean = false; + private logger: WebpackLogger | typeof console = console; + + constructor(path: string); + constructor(options: GeneratingConfiguration); + constructor(obj?: any) { + if (typeof obj == 'string') { + this.definitionsFile = obj; + } else { + try { + validateInputConfig(obj); + } catch (err) { + obj.logger.error(`invalid input webpack config object`); + if (err instanceof InvalidConfigurationError) { + obj.logger.error(err.validationErrors); + } else { + obj.logger.error(err); + } + throw err; + } + this.definitions = withDefaultConfig(obj); + } + } + + private static async readDefinitions( + obj: VSCodeExtensionsPackageJsonGenerator + ) { + if (!obj.definitionsFile) { + return false; + } + try { + obj.definitions = await readGeneratingConfiguration(obj.definitionsFile); + return true; + } catch (err) { + obj.logger.error(`invalid input webpack config object`); + if (err instanceof InvalidConfigurationError) { + obj.logger.error(err.validationErrors); + } else { + obj.logger.error(err); + } + return false; + } + } + + private static async updatePackageJson( + obj: VSCodeExtensionsPackageJsonGenerator + ) { + if (obj.needsUpdate && obj.definitions !== undefined) { + const { + configurations, prefix, templateFile, targetFile, tsconfig, tags, sort, + } = obj.definitions; + + const nextConfig = createConfigurationObject( + prefix, + configurations, + tsconfig, + tags, + sort + ); + + const packageJson: PackageJson = JSON.parse( + await fs.readFile(templateFile, 'utf8') + ); + // make sure contributes.configuration is defined + if (packageJson.contributes?.configuration === undefined) { + if (packageJson.contributes === undefined) + packageJson.contributes = { configuration: {} }; + else { + packageJson.contributes.configuration = {}; + } + } + for (const [key, value] of Object.entries(nextConfig)) { + packageJson.contributes.configuration[key] = value; + } + + obj.logger.info(`writing updated json to "${targetFile}"`); + await fs.writeFile( + targetFile, + JSON.stringify(packageJson, undefined, 2).concat('\n') + ); + + obj.needsUpdate = false; + } + } + + apply(compiler: Compiler) { + const thisObj = this; + this.logger = compiler.getInfrastructureLogger(PLUGIN); + + const updateDefinitions = async () => { + try { + thisObj.needsUpdate = !compiler.options.watch || + await VSCodeExtensionsPackageJsonGenerator.readDefinitions(thisObj); + await updatePackageJson(); + } catch (err: any) { + thisObj.logger.error(`error in ${thisObj.definitionsFile}`); + thisObj.logger.error(err.message); + return; + } + }; + const updatePackageJson = async () => { + try { + await VSCodeExtensionsPackageJsonGenerator.updatePackageJson(thisObj); + } catch (err: any) { + thisObj.logger.error(err.message); + return; + } + }; + + compiler.hooks.environment.tap(PLUGIN, async () => { + await updateDefinitions(); + }); + + // here I wanted to use the compiler.watchMode flag, but it seems to be `false` + // in all hooks before starting the compilation, so I resolved to use the watchRun hook. + let watcher: ReturnType | undefined; + compiler.hooks.watchRun.tap(PLUGIN, async () => { + if (!watcher && thisObj.definitionsFile) { + watcher = fs.watch(thisObj.definitionsFile); + for await (const event of watcher) { + if (event.eventType === 'change') { + await updateDefinitions(); + } + } + } + }); + + compiler.hooks.watchRun.tapPromise(PLUGIN, async (comp) => { + const ino = async (file: string) => { + return (await fs.stat(file)).ino; + }; + if (thisObj.definitions && comp.modifiedFiles) { + // check if modified file is definition file + const definitions = await Promise.all( + thisObj.definitions.configurations.map((c) => c.filePath).map(ino) + ); + const changedFiles = await Promise.all( + Array.from(comp.modifiedFiles, ino) + ); + thisObj.needsUpdate ||= + changedFiles.filter((i) => definitions.includes(i)).length > 0; + } + }); + + compiler.hooks.done.tap(PLUGIN, async () => { + await updatePackageJson(); + }); + } +} diff --git a/src/webpack/index.ts b/src/webpack/index.ts index 4ee490e..b36dfa0 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -1,172 +1,6 @@ -import { Compilation, Compiler } from 'webpack'; -import { createConfigurationObject } from '../vscode-extension-config'; -import * as fs from 'fs/promises'; -import { GeneratingConfiguration, PackageJson } from '../types'; -import { - InvalidConfigurationError, - readGeneratingConfiguration, - validateInputConfig, - withDefaultConfig, -} from '../input-configuration-helper'; -type WebpackLogger = ReturnType; +import { Compilation } from 'webpack'; +export type WebpackLogger = ReturnType; -const PLUGIN = 'VSCode Extension Config Generator'; +export const PLUGIN = 'VSCode Extension Config Generator'; -export class VSCodeExtensionsPackageJsonGenerator { - private readonly definitionsFile: string | undefined; - private definitions: Required | undefined; - private needsUpdate: boolean = false; - private logger: WebpackLogger | typeof console = console; - constructor(path: string); - constructor(options: GeneratingConfiguration); - constructor(obj?: any) { - if (typeof obj == 'string') { - this.definitionsFile = obj; - } else { - try { - validateInputConfig(obj); - } catch (err) { - obj.logger.error(`invalid input webpack config object`); - if (err instanceof InvalidConfigurationError) { - obj.logger.error(err.validationErrors); - } else { - obj.logger.error(err); - } - throw err; - } - this.definitions = withDefaultConfig(obj); - } - } - - private static async readDefinitions( - obj: VSCodeExtensionsPackageJsonGenerator - ) { - if (!obj.definitionsFile) { - return false; - } - try { - obj.definitions = await readGeneratingConfiguration(obj.definitionsFile); - return true; - } catch (err) { - obj.logger.error(`invalid input webpack config object`); - if (err instanceof InvalidConfigurationError) { - obj.logger.error(err.validationErrors); - } else { - obj.logger.error(err); - } - return false; - } - } - - private static async updatePackageJson( - obj: VSCodeExtensionsPackageJsonGenerator - ) { - if (obj.needsUpdate && obj.definitions !== undefined) { - const { - configurations, - prefix, - templateFile, - targetFile, - tsconfig, - tags, - sort, - } = obj.definitions; - - const nextConfig = createConfigurationObject( - prefix, - configurations, - tsconfig, - tags, - sort - ); - - const packageJson: PackageJson = JSON.parse( - await fs.readFile(templateFile, 'utf8') - ); - // make sure contributes.configuration is defined - if (packageJson.contributes?.configuration === undefined) { - if (packageJson.contributes === undefined) - packageJson.contributes = { configuration: {} }; - else { - packageJson.contributes.configuration = {}; - } - } - for (const [key, value] of Object.entries(nextConfig)) { - packageJson.contributes.configuration[key] = value; - } - - obj.logger.info(`writing updated json to "${targetFile}"`); - await fs.writeFile( - targetFile, - JSON.stringify(packageJson, undefined, 2).concat('\n') - ); - - obj.needsUpdate = false; - } - } - - apply(compiler: Compiler) { - const thisObj = this; - this.logger = compiler.getInfrastructureLogger(PLUGIN); - - const updateDefinitions = async () => { - try { - thisObj.needsUpdate = !compiler.options.watch || - await VSCodeExtensionsPackageJsonGenerator.readDefinitions(thisObj); - await updatePackageJson(); - } catch (err: any) { - thisObj.logger.error(`error in ${thisObj.definitionsFile}`); - thisObj.logger.error(err.message); - return; - } - }; - const updatePackageJson = async () => { - try { - await VSCodeExtensionsPackageJsonGenerator.updatePackageJson(thisObj); - } catch (err: any) { - thisObj.logger.error(err.message); - return; - } - }; - - compiler.hooks.environment.tap(PLUGIN, async () => { - await updateDefinitions(); - }); - - // here I wanted to use the compiler.watchMode flag, but it seems to be `false` - // in all hooks before starting the compilation, so I resolved to use the watchRun hook. - let watcher: ReturnType | undefined; - compiler.hooks.watchRun.tap(PLUGIN, async () => { - if (!watcher && thisObj.definitionsFile) { - watcher = fs.watch(thisObj.definitionsFile); - for await (const event of watcher) { - if (event.eventType === 'change') { - await updateDefinitions(); - } - } - } - }); - - compiler.hooks.watchRun.tapPromise(PLUGIN, async (comp) => { - const ino = async (file: string) => { - return (await fs.stat(file)).ino; - }; - if (thisObj.definitions && comp.modifiedFiles) { - // check if modified file is definition file - const definitions = await Promise.all( - thisObj.definitions.configurations.map((c) => c.filePath).map(ino) - ); - const changedFiles = await Promise.all( - Array.from(comp.modifiedFiles, ino) - ); - thisObj.needsUpdate ||= - changedFiles.filter((i) => definitions.includes(i)).length > 0; - } - }); - - compiler.hooks.done.tap(PLUGIN, async () => { - await updatePackageJson(); - }); - } -}