-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revert "vscode files" #7
Open
SonnyMoore37
wants to merge
3
commits into
elazarcoh:main
Choose a base branch
from
SonnyMoore37:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +0,0 @@ | ||
{ | ||
"recommendations": [ | ||
"esbenp.prettier-vscode", | ||
"visualstudioexptteam.vscodeintellicode", | ||
"dbaeumer.vscode-eslint" | ||
] | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GeneratingConfiguration> | 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<typeof fs.watch> | 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(); | ||
}); | ||
} | ||
} |
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,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<Compilation['getLogger']>; | ||
import { Compilation } from 'webpack'; | ||
export type WebpackLogger = ReturnType<Compilation['getLogger']>; | ||
|
||
const PLUGIN = 'VSCode Extension Config Generator'; | ||
export const PLUGIN = 'VSCode Extension Config Generator'; | ||
|
||
export class VSCodeExtensionsPackageJsonGenerator { | ||
private readonly definitionsFile: string | undefined; | ||
private definitions: Required<GeneratingConfiguration> | 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<typeof fs.watch> | 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(); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any