Skip to content

Commit

Permalink
Add watch marker plugin.
Browse files Browse the repository at this point in the history
Change cache type.
  • Loading branch information
Codeneos committed Nov 4, 2021
1 parent 3462800 commit 3ebb385
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 109 deletions.
43 changes: 28 additions & 15 deletions build/buildCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@ interface CommandInfo {
menus?: CommandMenuInfo[];
}

interface CommandsYaml {
export interface CommandsYaml {
[commandName: string]: CommandInfo;
}

interface PackageContributions {
commands: {
command: string;
title: string;
category?: string;
enablement?: string;
icon?: string | { light?: string; dark?: string };
}[];
commands: PackageContributionCommand[];
menus: {
[name: string]: {
command: string;
Expand All @@ -56,6 +50,19 @@ interface PackageContributions {
};
}

interface PackageContributionCommand {
command: string;
title: string;
category?: string;
enablement?: string;
icon?: string | { light?: string; dark?: string };
}

export interface PackageJson {
contributes: PackageContributions;
activationEvents?: string[];
}

async function loadCommandsMeta(yamlFile: string): Promise<CommandsYaml> {
return yaml.load((await fs.readFile(yamlFile)).toString()) as CommandsYaml;
}
Expand All @@ -71,20 +78,18 @@ function stripUndefined<T extends object>(obj: T) : T {
return obj;
}

async function updatePackageJson(packageJsonFile: string, commandFile: string) {
export function mergeCommandContributions(packageJson: PackageJson, commands: CommandsYaml): PackageJson {
console.log('Adding VSCode commands to package...');

const packageJson = await fs.readJSON(packageJsonFile);
const commands = await loadCommandsMeta(commandFile);
const contributes : PackageContributions = { commands: [], menus: {} };
const activationEvents = new Set<string>(packageJson.activationEvents || []);

for (const [name, command] of Object.entries(commands)) {
// Add command base structure
const newCommand : any = {
const newCommand: PackageContributionCommand = {
command: name,
title: command.title,
groucategoryp: command.category
category: command.category
};

if (command.icon) {
Expand Down Expand Up @@ -139,11 +144,11 @@ async function updatePackageJson(packageJsonFile: string, commandFile: string) {
// Update package JSON
packageJson.activationEvents = [...activationEvents];
packageJson.contributes = {...packageJson.contributes, ...contributes};
await fs.writeJSON(packageJsonFile, packageJson, { spaces: 4 });
console.log(`${chalk.bold(logSymbols.success)} Added ${chalk.bold(Object.entries(commands).length)} commands`);
return packageJson;
}

async function updateCommandsYaml(packageJsonFile: string, commandFile: string) {
export async function updateCommandsYaml(packageJsonFile: string, commandFile: string) {
const packageJson = await fs.readJSON(packageJsonFile);
const contributes : PackageContributions = packageJson.contributes;
const commands : CommandsYaml = {};
Expand Down Expand Up @@ -187,6 +192,14 @@ async function updateCommandsYaml(packageJsonFile: string, commandFile: string)
await fs.writeFile(commandFile, yaml.dump(commands));
}

async function updatePackageJson(packageJsonFile: string, commandFile: string) {
const packageJson = await fs.readJSON(packageJsonFile);
const commands = await loadCommandsMeta(commandFile);
mergeCommandContributions(packageJson, commands);
// Update package JSON
await fs.writeJSON(packageJsonFile, packageJson, { spaces: 4 });
}

// Run update command
const [ packageJson, commandsYaml ] = process.argv.slice(2);
updatePackageJson(packageJson, commandsYaml);
Expand Down
3 changes: 3 additions & 0 deletions build/loaders/prefixTransform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = function (source) {
if (this.cacheable) {
this.cacheable(true);
}
return source.replace(/ from 'node:([a-zA-Z]+)'/ig, " from '$1'");
};
2 changes: 1 addition & 1 deletion build/loaders/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function loadYamlFile(yamlFile) {

module.exports = function (source) {
if (this.cacheable) {
this.cacheable();
this.cacheable(true);
}

const value = JSON.stringify(loadYaml(source))
Expand Down
21 changes: 21 additions & 0 deletions build/plugins/watchMarkers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { WebpackPluginInstance, Compiler } from 'webpack';

export default class WatchMarkersPlugin implements WebpackPluginInstance {
private buildCounter = 0;

public apply(compiler: Compiler) {
compiler.hooks.infrastructureLog.tap('WatchMarkersPlugin', (arg0, arg1, arg2) => {
const messages = arg2.filter(m => typeof m === 'string');
const isCompileStart = messages.some(msg => /Compiler '[\w\d]+' starting... /i.test(msg));
const isCompileEnd = messages.some(msg => msg.endsWith('watching files for updates...'));

if (isCompileStart && this.buildCounter++ == 0 ) {
console.debug('Build starting...');
} else if (isCompileEnd && --this.buildCounter == 0 ) {
console.debug('Build completed!');
}

return true;
});
}
}
Loading

0 comments on commit 3ebb385

Please sign in to comment.