refactor(ivy): ngcc - simplify `NewEntryPointFileWriter` code (#30085)

The lines that compute the paths for this writer were confusing.
This commit simplifies and clarifies what is being computed.

PR Close #30085
This commit is contained in:
Pete Bacon Darwin 2019-04-24 15:39:38 +01:00 committed by Andrew Kushnir
parent 6af9b8fb92
commit 0fa76219ac
1 changed files with 16 additions and 18 deletions

View File

@ -32,46 +32,44 @@ const NGCC_DIRECTORY = '__ivy_ngcc__';
export class NewEntryPointFileWriter extends InPlaceFileWriter { export class NewEntryPointFileWriter extends InPlaceFileWriter {
writeBundle(entryPoint: EntryPoint, bundle: EntryPointBundle, transformedFiles: FileInfo[]) { writeBundle(entryPoint: EntryPoint, bundle: EntryPointBundle, transformedFiles: FileInfo[]) {
// The new folder is at the root of the overall package // The new folder is at the root of the overall package
const relativeEntryPointPath = relative(entryPoint.package, entryPoint.path); const ngccFolder = AbsoluteFsPath.fromUnchecked(join(entryPoint.package, NGCC_DIRECTORY));
const relativeNewDir = join(NGCC_DIRECTORY, relativeEntryPointPath); this.copyBundle(bundle, entryPoint.package, ngccFolder);
const newDir = AbsoluteFsPath.fromUnchecked(join(entryPoint.package, relativeNewDir)); transformedFiles.forEach(file => this.writeFile(file, entryPoint.package, ngccFolder));
this.copyBundle(bundle, entryPoint.package, entryPoint.path, newDir); this.updatePackageJson(entryPoint, bundle.formatProperty, ngccFolder);
transformedFiles.forEach(file => this.writeFile(file, entryPoint.path, newDir));
this.updatePackageJson(entryPoint, bundle.formatProperty, newDir);
} }
protected copyBundle( protected copyBundle(
bundle: EntryPointBundle, packagePath: AbsoluteFsPath, entryPointPath: AbsoluteFsPath, bundle: EntryPointBundle, packagePath: AbsoluteFsPath, ngccFolder: AbsoluteFsPath) {
newDir: AbsoluteFsPath) {
bundle.src.program.getSourceFiles().forEach(sourceFile => { bundle.src.program.getSourceFiles().forEach(sourceFile => {
const isOutsidePackage = relative(packagePath, sourceFile.fileName).startsWith('..'); const relativePath = relative(packagePath, sourceFile.fileName);
const isOutsidePackage = relativePath.startsWith('..');
if (!sourceFile.isDeclarationFile && !isOutsidePackage) { if (!sourceFile.isDeclarationFile && !isOutsidePackage) {
const relativePath = relative(entryPointPath, sourceFile.fileName); const newFilePath = join(ngccFolder, relativePath);
const newFilePath = join(newDir, relativePath);
mkdir('-p', dirname(newFilePath)); mkdir('-p', dirname(newFilePath));
cp(sourceFile.fileName, newFilePath); cp(sourceFile.fileName, newFilePath);
} }
}); });
} }
protected writeFile(file: FileInfo, entryPointPath: AbsoluteFsPath, newDir: AbsoluteFsPath): protected writeFile(file: FileInfo, packagePath: AbsoluteFsPath, ngccFolder: AbsoluteFsPath):
void { void {
if (isDtsPath(file.path.replace(/\.map$/, ''))) { if (isDtsPath(file.path.replace(/\.map$/, ''))) {
// This is either `.d.ts` or `.d.ts.map` file // This is either `.d.ts` or `.d.ts.map` file
super.writeFileAndBackup(file); super.writeFileAndBackup(file);
} else { } else {
const relativePath = relative(entryPointPath, file.path); const relativePath = relative(packagePath, file.path);
const newFilePath = join(newDir, relativePath); const newFilePath = join(ngccFolder, relativePath);
mkdir('-p', dirname(newFilePath)); mkdir('-p', dirname(newFilePath));
writeFileSync(newFilePath, file.contents, 'utf8'); writeFileSync(newFilePath, file.contents, 'utf8');
} }
} }
protected updatePackageJson( protected updatePackageJson(
entryPoint: EntryPoint, formatProperty: EntryPointJsonProperty, newDir: AbsoluteFsPath) { entryPoint: EntryPoint, formatProperty: EntryPointJsonProperty, ngccFolder: AbsoluteFsPath) {
const bundlePath = entryPoint.packageJson[formatProperty] !; const formatPath = join(entryPoint.path, entryPoint.packageJson[formatProperty] !);
const newBundlePath = relative(entryPoint.path, join(newDir, bundlePath)); const newFormatPath = join(ngccFolder, relative(entryPoint.package, formatPath));
(entryPoint.packageJson as any)[formatProperty + '_ivy_ngcc'] = newBundlePath; const newFormatProperty = formatProperty + '_ivy_ngcc';
(entryPoint.packageJson as any)[newFormatProperty] = relative(entryPoint.path, newFormatPath);
writeFileSync(join(entryPoint.path, 'package.json'), JSON.stringify(entryPoint.packageJson)); writeFileSync(join(entryPoint.path, 'package.json'), JSON.stringify(entryPoint.packageJson));
} }
} }