Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
{
"recommendations": [
"esbenp.prettier-vscode",
"visualstudioexptteam.vscodeintellicode",
"dbaeumer.vscode-eslint"
]
}
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "modificationsIfAvailable",
"cSpell.enabled": true,
"cSpell.words": [
"vsix"
],
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
165 changes: 165 additions & 0 deletions src/webpack/VSCodeExtensionsPackageJsonGenerator.ts
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) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any

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();
});
}
}
172 changes: 3 additions & 169 deletions src/webpack/index.ts
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();
});
}
}