Skip to content

Commit

Permalink
feat(ProjectParser): add typeDocJsonParserVersion property
Browse files Browse the repository at this point in the history
  • Loading branch information
RealShadowNova committed Jul 30, 2022
1 parent 99de280 commit 901d9ec
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"format": "prettier --write src/**/*.ts",
"test": "jest",
"test:coverage": "jest --coverage",
"build": "tsc -b src && gen-esm-wrapper dist/index.js dist/index.mjs",
"build": "rimraf dist && tsc -b src && gen-esm-wrapper dist/index.js dist/index.mjs && node scripts/versionInjector.mjs",
"update": "yarn upgrade-interactive",
"bump": "cliff-jumper",
"check-update": "cliff-jumper --dry-run",
Expand Down
26 changes: 26 additions & 0 deletions scripts/versionInjector.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { cyan, green } from 'colorette';
import { readdir, readFile, stat, writeFile } from 'node:fs/promises';
import { resolve, sep } from 'node:path';

const packageJson = JSON.parse(await readFile(resolve(process.cwd(), 'package.json'), 'utf8'));

async function versionInjector(path) {
const fileNames = await readdir(resolve(path));

for (const fileName of fileNames) {
const file = await stat(resolve(path, fileName));

if (file.isDirectory()) await versionInjector(resolve(path, fileName));
else if (file.isFile() && fileName.endsWith('.js')) {
const content = await readFile(resolve(path, fileName), 'utf8');

if (content.includes('[@versionInjector]')) {
console.log(`${green('[@versionInjector]')} ${cyan(`${path}${sep}${fileName}`)}`);

await writeFile(resolve(path, fileName), content.replace(/\[@versionInjector\]/g, packageJson.version));
}
}
}
}

await versionInjector(resolve(process.cwd(), 'dist'));
16 changes: 15 additions & 1 deletion src/lib/structures/ProjectParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import { TypeAliasParser } from './TypeAliasParser';
* @since 1.0.0
*/
export class ProjectParser {
/**
* The version of `typedoc-json-parser` used to generate this project.
* @since 1.0.0
*/
public readonly typeDocJsonParserVersion: string = '[@versionInjector]';

/**
* The identifier of this project. This is usually `0`
* @since 1.0.0
Expand Down Expand Up @@ -76,8 +82,9 @@ export class ProjectParser {
this.name = name;

if ('classes' in data) {
const { classes, constants, enums, functions, interfaces, namespaces, typeAliases } = data;
const { typeDocJsonParserVersion, classes, constants, enums, functions, interfaces, namespaces, typeAliases } = data;

this.typeDocJsonParserVersion = typeDocJsonParserVersion;
this.classes = classes.map((json) => ClassParser.generateFromJSON(json, this));
this.constants = constants.map((json) => ConstantParser.generateFromJSON(json, this));
this.enums = enums.map((json) => EnumParser.generateFromJSON(json, this));
Expand Down Expand Up @@ -117,6 +124,7 @@ export class ProjectParser {
*/
public toJSON(): ProjectParser.JSON {
return {
typeDocJsonParserVersion: this.typeDocJsonParserVersion,
id: this.id,
name: this.name,
classes: this.classes.map((parser) => parser.toJSON()),
Expand All @@ -132,6 +140,12 @@ export class ProjectParser {

export namespace ProjectParser {
export interface JSON {
/**
* The version of `typedoc-json-parser` that generated this JSON object.
* @since 2.1.0
*/
typeDocJsonParserVersion: string;

/**
* The identifier of this project. This is usually `0`
* @since 1.0.0
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.base.json",
"include": ["jest.config.mjs", "src", "tests"]
"include": ["jest.config.mjs", "src", "tests", "scripts"]
}

0 comments on commit 901d9ec

Please sign in to comment.