Skip to content

Commit

Permalink
chore: generate new type files before removing new files
Browse files Browse the repository at this point in the history
  • Loading branch information
rainx committed Sep 18, 2023
1 parent 0da15cf commit fa24a09
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions src/php-parser/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
mkdirSync,
readFileSync,
readdirSync,
renameSync,
rmSync,
statSync,
writeFileSync,
} from 'fs';
import * as os from 'os';
import { resolve } from 'path';
import * as path from 'path';
import * as _ from 'lodash';
Expand Down Expand Up @@ -58,6 +60,7 @@ export interface ICliContext {
allNodes: {
[nodeType: string]: IContextNodeItem;
};
temporaryNodeTypeRootPath: string;
}

class GenerateType {
Expand All @@ -67,15 +70,17 @@ class GenerateType {
private static cliContext: ICliContext = {
allNodeNames: [],
allNodes: {},
temporaryNodeTypeRootPath: resolve(os.tmpdir(), 'node'),
};

public static main(): void {
const typesRootPath = FilePathHelpers.getTypesRootPath();
const nodeTypeRootPath = resolve(typesRootPath, 'node');

// Remove and create a node root path first
rmSync(nodeTypeRootPath, { recursive: true, force: true });
mkdirSync(nodeTypeRootPath, { recursive: true });
mkdirSync(GenerateType.cliContext.temporaryNodeTypeRootPath, {
recursive: true,
});

GenerateType.walkSync(
PHP_PARSER_NODE_DIRECTORY_PATH,
Expand All @@ -84,13 +89,25 @@ class GenerateType {
);

const typesFileName = resolve(typesRootPath, 'types.ts');
const temporaryTypesFileName = resolve(typesRootPath, 'tmp-types.ts');
writeFileSync(
typesFileName,
temporaryTypesFileName,
TypeGenerationHelpers.generateCombinationTypesFromNodes(
GenerateType.cliContext,
),
{ encoding: 'utf-8' },
);

// move temporary generated files to correct path
// we need to remove the outdated files first
rmSync(nodeTypeRootPath, { recursive: true, force: true });
rmSync(typesFileName, { force: true });
// move them
renameSync(
GenerateType.cliContext.temporaryNodeTypeRootPath,
nodeTypeRootPath,
);
renameSync(temporaryTypesFileName, typesFileName);
}

private static generateTypeForNodeByFile(filePath: string): void {
Expand Down Expand Up @@ -175,19 +192,27 @@ class GenerateType {
SRC_ROOT,
'templates/php-parser-node.mustache',
);
const typesRootPath = FilePathHelpers.getTypesRootPath();
const nodeTypeRootPath = resolve(typesRootPath, 'node');
const templateFileContent = readFileSync(templateFileName, 'utf-8');

try {
if (directoryRelativePath) {
mkdirSync(path.resolve(nodeTypeRootPath, directoryRelativePath), {
recursive: true,
});
mkdirSync(
path.resolve(
GenerateType.cliContext.temporaryNodeTypeRootPath,
directoryRelativePath,
),
{
recursive: true,
},
);
}
} finally {
writeFileSync(
resolve(nodeTypeRootPath, directoryRelativePath, `${fileName}.ts`),
resolve(
GenerateType.cliContext.temporaryNodeTypeRootPath,
directoryRelativePath,
`${fileName}.ts`,
),

Mustache.render(templateFileContent, nodeOutput),
);
Expand Down

0 comments on commit fa24a09

Please sign in to comment.