Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
162 lines (144 loc) · 4.43 KB
/
Copy pathutils.ts
File metadata and controls
162 lines (144 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import execa from 'execa';
import { promises as fs } from 'fs';
import path from 'path';
import type { PackageType } from './constants';
import { MonorepoFiles, Placeholders, TemplateDirectories } from './constants';
import type { FileMap } from './fs-utils';
import { readAllFiles, writeFiles } from './fs-utils';
const REPO_ROOT = path.join(__dirname, '..', '..');
const REPO_PACKAGE_JSON = path.join(REPO_ROOT, MonorepoFiles.PackageJson);
const PACKAGES_PATH = path.join(REPO_ROOT, 'packages');
const allPlaceholdersRegex = new RegExp(
Object.values(Placeholders).join('|'),
'gu',
);
/**
* The data necessary to create a new package.
*/
export type PackageData = Readonly<{
name: string;
description: string;
type: PackageType;
directoryName: string;
nodeVersions: string;
currentYear: string;
}>;
/**
* Data parsed from relevant monorepo files.
*/
type MonorepoFileData = {
nodeVersions: string;
};
/**
* A parsed package.json file.
*/
type PackageJson = {
engines: { node: string };
[key: string]: unknown;
};
/**
* Reads the monorepo files that need to be parsed or modified.
*
* @returns A map of file paths to file contents.
*/
export async function readMonorepoFiles(): Promise<MonorepoFileData> {
const packageJson = await fs.readFile(REPO_PACKAGE_JSON, 'utf-8');
return {
nodeVersions: (JSON.parse(packageJson) as PackageJson).engines.node,
};
}
/**
* Finalizes package and repo files, writes them to disk, and performs necessary
* postprocessing (e.g. running `yarn install`).
*
* @param packageData - The package data.
* @param monorepoFileData - The monorepo file data.
*/
export async function finalizeAndWriteData(
packageData: PackageData,
monorepoFileData: MonorepoFileData,
): Promise<void> {
const packagePath = path.join(PACKAGES_PATH, packageData.directoryName);
try {
await fs.stat(packagePath);
throw new Error(`The package directory already exists: ${packagePath}`);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw error;
}
}
console.log('Writing package files...');
// Read and write package files
await writeFiles(
packagePath,
await processTemplateFiles(packageData, monorepoFileData),
);
// Postprocess
// Add the new package to the lockfile.
console.log('Running "yarn install"...');
await execa('yarn', ['install'], { cwd: REPO_ROOT });
// Add the new package to the root readme content
console.log('Running "yarn readme-content:update"...');
await execa('yarn', ['readme-content:update'], { cwd: REPO_ROOT });
}
/**
* Reads the template files for the package type and updates them with the
* specified package data.
*
* @param packageData - The package data.
* @param monorepoFileData - The monorepo file data.
* @returns A map of file paths to processed template file contents.
*/
async function processTemplateFiles(
packageData: PackageData,
monorepoFileData: MonorepoFileData,
): Promise<FileMap> {
const result: FileMap = {};
const templateDir = path.join(
__dirname,
TemplateDirectories[packageData.type],
);
const templateFiles = await readAllFiles(templateDir);
for (const [relativePath, content] of Object.entries(templateFiles)) {
result[relativePath] = processTemplateContent(
packageData,
monorepoFileData,
content,
);
}
return result;
}
/**
* Processes the template file content by replacing placeholders with relevant values
* from the specified package data.
*
* @param packageData - The package data.
* @param monorepoFileData - The monorepo file data.
* @param content - The template file content.
* @returns The processed template file content.
*/
function processTemplateContent(
packageData: PackageData,
monorepoFileData: MonorepoFileData,
content: string,
): string {
const { name, description, currentYear } = packageData;
const { nodeVersions } = monorepoFileData;
return content.replace(allPlaceholdersRegex, (match) => {
switch (match) {
case Placeholders.CurrentYear:
return currentYear;
case Placeholders.NodeVersions:
return nodeVersions;
case Placeholders.PackageName:
return name;
case Placeholders.PackageDescription:
return description;
case Placeholders.PackageDirectoryName:
return packageData.directoryName;
/* istanbul ignore next: should be impossible */
default:
throw new Error(`Unknown placeholder: ${match}`);
}
});
}