-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nx): generators correctly respect “dry-run”
Without this change we would write the files even in dry-mode. This meant that the “preview” of the generate UI would change files!!
- Loading branch information
Showing
5 changed files
with
133 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as path from 'path'; | ||
|
||
import { Tree, generateFiles } from '@nx/devkit'; | ||
import fs from 'fs-extra'; | ||
|
||
type GenerateFilesFunction = ( | ||
substitutions: Parameters<typeof generateFiles>[3], | ||
) => void; | ||
|
||
export function createTempFiles({ | ||
tree, | ||
projectRoot, | ||
cacheContent, | ||
cacheLocation, | ||
createFiles, | ||
}: { | ||
tree: Tree; | ||
projectRoot: string; | ||
cacheContent?: string; | ||
cacheLocation: string; | ||
createFiles: (templatePath: string) => void; | ||
}): GenerateFilesFunction { | ||
const cachePath = path.join(cacheLocation, '.cache'); | ||
const templatePath = path.join(cacheLocation, 'files'); | ||
const generateFunction: GenerateFilesFunction = (substitutions) => | ||
generateFiles( | ||
tree, | ||
templatePath, | ||
path.relative(tree.root, projectRoot), | ||
substitutions, | ||
); | ||
if (cacheContent) { | ||
if (fs.existsSync(cachePath)) { | ||
const cache = fs.readFileSync(cachePath, 'utf-8'); | ||
if (cache === cacheContent) { | ||
return generateFunction; | ||
} | ||
} | ||
fs.writeFileSync(cachePath, cacheContent); | ||
} | ||
fs.emptyDirSync(templatePath); | ||
createFiles(templatePath); | ||
return generateFunction; | ||
} |