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