nx/packages/workspace/src/generators/move/lib/update-project-root-files.ts
Phillip Barta 52e3083a19
feat(core): added encoding param in Tree.read() (#5668)
feat(core): added encoding param in Tree.read()
2021-05-17 21:03:04 -04:00

51 lines
1.4 KiB
TypeScript

import * as path from 'path';
import { ProjectConfiguration, Tree } from '@nrwl/devkit';
import { appRootPath } from '../../../utilities/app-root';
import { Schema } from '../schema';
import { getDestination } from './utils';
import { extname, join } from 'path';
/**
* Updates the files in the root of the project
*
* Typically these are config files which point outside of the project folder
*
* @param schema The options provided to the schematic
*/
export function updateProjectRootFiles(
tree: Tree,
schema: Schema,
project: ProjectConfiguration
) {
const destination = getDestination(tree, schema, project);
const newRelativeRoot = path
.relative(path.join(appRootPath, destination), appRootPath)
.split(path.sep)
.join('/');
const oldRelativeRoot = path
.relative(path.join(appRootPath, project.root), appRootPath)
.split(path.sep)
.join('/');
if (newRelativeRoot === oldRelativeRoot) {
// nothing to do
return;
}
const dots = /\./g;
const regex = new RegExp(oldRelativeRoot.replace(dots, '\\.'), 'g');
for (const file of tree.children(destination)) {
if (!extname(file).startsWith('.js')) {
continue;
}
const oldContent = tree.read(join(destination, file), 'utf-8');
const newContent = oldContent.replace(regex, newRelativeRoot);
tree.write(join(destination, file), newContent);
}
}