All files / create-package/src utils.ts

100% Statements 50/50
100% Branches 10/10
100% Functions 10/10
100% Lines 48/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                      2x   2x 2x 2x 2x 2x   2x           2x                                                                       1x         1x                                 4x 4x 1x         2x     2x     2x 2x       2x                 2x 2x         2x 2x 2x       1x                         4x 4x 1x   3x 2x     1x                           4x                               2x   2x     2x   2x         2x                       2x 2x   2x 10x     2x                             10x   10x 16x   4x   4x   4x   4x              
import { execa } from 'execa';
import { promises as fs } from 'node:fs';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { format as prettierFormat } from 'prettier';
import type { Options as PrettierOptions } from 'prettier';
 
import { MonorepoFile, Placeholder } from './constants.ts';
import type { FileMap } from './fs-utils.ts';
import { readAllFiles, writeFiles } from './fs-utils.ts';
 
const currentDir = dirname(fileURLToPath(import.meta.url));
 
const PACKAGE_TEMPLATE_DIR = path.join(currentDir, '../../template-package');
const REPO_ROOT = path.join(currentDir, '..', '..', '..');
const REPO_TS_CONFIG = path.join(REPO_ROOT, MonorepoFile.TsConfig);
const REPO_TS_CONFIG_BUILD = path.join(REPO_ROOT, MonorepoFile.TsConfigBuild);
const PACKAGES_PATH = path.join(REPO_ROOT, 'packages');
 
const allPlaceholdersRegex = new RegExp(
  Object.values(Placeholder).join('|'),
  'gu',
);
 
// Our lint config really hates this, but it works.
const prettierRc = (await import(
  path.join(REPO_ROOT, '.prettierrc.cjs')
)) as PrettierOptions;
 
/**
 * The data necessary to create a new package.
 */
export type PackageData = Readonly<{
  name: string;
  description: string;
  directoryName: string;
  currentYear: string;
}>;
 
/**
 * Data parsed from relevant monorepo files.
 */
export type MonorepoFileData = {
  tsConfig: Tsconfig;
  tsConfigBuild: Tsconfig;
};
 
/**
 * A parsed tsconfig file.
 */
type Tsconfig = {
  references: { path: 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 [tsConfig, tsConfigBuild] = await Promise.all([
    fs.readFile(REPO_TS_CONFIG, 'utf-8'),
    fs.readFile(REPO_TS_CONFIG_BUILD, 'utf-8'),
  ]);
 
  return {
    tsConfig: JSON.parse(tsConfig) as Tsconfig,
    tsConfigBuild: JSON.parse(tsConfigBuild) as Tsconfig,
  };
}
 
/**
 * 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);
  if (await exists(packagePath)) {
    throw new Error(`The package directory already exists: ${packagePath}`);
  }
 
  // TODO(#562): Use logger instead or change lint rule.
  // eslint-disable-next-line no-console
  console.log('Writing package and monorepo files...');
 
  // Read and write package files
  await writeFiles(packagePath, await processTemplateFiles(packageData));
 
  // Write monorepo files
  updateTsConfigs(packageData, monorepoFileData);
  await writeJsonFile(
    REPO_TS_CONFIG,
    JSON.stringify(monorepoFileData.tsConfig),
  );
  await writeJsonFile(
    REPO_TS_CONFIG_BUILD,
    JSON.stringify(monorepoFileData.tsConfigBuild),
  );
 
  // Postprocess
  // Add the new package to the lockfile.
  // TODO(#562): Use logger instead or change lint rule.
  // eslint-disable-next-line no-console
  console.log('Running "yarn install"...');
  await execa('yarn', ['install'], { cwd: REPO_ROOT });
 
  // Run constraints
  // TODO(#562): Use logger instead or change lint rule.
  // eslint-disable-next-line no-console
  console.log('Running "yarn constraints --fix"...');
  try {
    await execa('yarn', ['constraints', '--fix'], { cwd: REPO_ROOT });
  } catch {
    // TODO(#562): Use logger instead or change lint rule.
    // eslint-disable-next-line no-console
    console.error(
      'Warning: Failed to run "yarn constraints --fix". You will need to re-run it manually.',
    );
  }
}
 
/**
 * Checks if a file exists.
 *
 * @param filePath - The absolute path of the file to check.
 * @returns Whether the file exists.
 */
async function exists(filePath: string): Promise<boolean> {
  try {
    await fs.access(filePath);
    return true;
  } catch (error: unknown) {
    if (error instanceof Error && 'code' in error) {
      return error.code !== 'ENOENT';
    }
    // Unexpected error
    throw error;
  }
}
 
/**
 * Formats a JSON file with `prettier` and writes it to disk.
 *
 * @param filePath - The absolute path of the file to write.
 * @param fileContent - The file content to write.
 */
async function writeJsonFile(
  filePath: string,
  fileContent: string,
): Promise<void> {
  await fs.writeFile(
    filePath,
    await prettierFormat(fileContent, { ...prettierRc, parser: 'json' }),
  );
}
 
/**
 * Updates the tsconfig file data in place to include the new package.
 *
 * @param packageData - = The package data.
 * @param monorepoFileData - The monorepo file data.
 */
function updateTsConfigs(
  packageData: PackageData,
  monorepoFileData: MonorepoFileData,
): void {
  const { tsConfig, tsConfigBuild } = monorepoFileData;
 
  tsConfig.references.push({
    path: `./${path.basename(PACKAGES_PATH)}/${packageData.directoryName}`,
  });
  tsConfig.references.sort((a, b) => a.path.localeCompare(b.path));
 
  tsConfigBuild.references.push({
    path: `./${path.basename(PACKAGES_PATH)}/${
      packageData.directoryName
    }/tsconfig.build.json`,
  });
  tsConfigBuild.references.sort((a, b) => a.path.localeCompare(b.path));
}
 
/**
 * Reads the template files and updates them with the specified package data.
 *
 * @param packageData - The package data.
 * @returns A map of file paths to processed template file contents.
 */
async function processTemplateFiles(
  packageData: PackageData,
): Promise<FileMap> {
  const result: FileMap = {};
  const templateFiles = await readAllFiles(PACKAGE_TEMPLATE_DIR);
 
  for (const [relativePath, content] of Object.entries(templateFiles)) {
    result[relativePath] = processTemplateContent(packageData, 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 content - The template file content.
 * @returns The processed template file content.
 */
function processTemplateContent(
  packageData: PackageData,
  content: string,
): string {
  const { name, description, currentYear } = packageData;
 
  return content.replace(allPlaceholdersRegex, (match) => {
    switch (match as Placeholder) {
      case Placeholder.CurrentYear:
        return currentYear;
      case Placeholder.PackageName:
        return name;
      case Placeholder.PackageDescription:
        return description;
      case Placeholder.PackageDirectoryName:
        return packageData.directoryName;
      /* istanbul ignore next: should be impossible */
      default:
        throw new Error(`Unknown placeholder: ${match}`);
    }
  });
}