Skip to content

Commit

Permalink
fix(tools): move inline templates that cause dep graph resolution iss…
Browse files Browse the repository at this point in the history
…ues to template files
  • Loading branch information
Hotell committed May 18, 2023
1 parent b6714af commit acb4830
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 30 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"@types/copy-webpack-plugin": "6.4.0",
"@types/dedent": "0.7.0",
"@types/doctrine": "0.0.5",
"@types/ejs": "3.1.2",
"@types/enzyme": "3.10.7",
"@types/eslint": "7.2.13",
"@types/express": "4.17.15",
Expand Down Expand Up @@ -221,6 +222,7 @@
"doctoc": "2.0.1",
"doctrine": "3.0.0",
"dotparser": "1.1.1",
"ejs": "3.1.9",
"enquirer": "2.3.6",
"enzyme": "3.10.0",
"enzyme-to-json": "3.6.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { preset } from '@fluentui/scripts-tasks';

preset();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { preset, task } from '@fluentui/scripts-tasks';

preset();

task('build', 'build:react-components').cached?.();`
38 changes: 15 additions & 23 deletions tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe('migrate-converged-pkg generator', () => {
it(`should remove @ts-ignore pragmas from all stories`, async () => {
const { paths } = setup({ createDummyStories: true });
// this is needed to stop TS parsing static imports and evaluating them in nx dep graph tree as true dependency - https://github.com/nrwl/nx/issues/8938
const template = fs.readFileSync(path.join(__dirname, '__fixtures__', 'ts-ignore-story.ts__tmpl__'), 'utf-8');
const template = getFixture('ts-ignore-story.ts__tmpl__');
append(tree, paths.storyOne, template);

await generator(tree, options);
Expand Down Expand Up @@ -1066,23 +1066,13 @@ describe('migrate-converged-pkg generator', () => {
const projectConfig = readProjectConfiguration(tree, options.name);
let justConfig = getJustConfig(projectConfig);

expect(justConfig).toMatchInlineSnapshot(`
"import { preset } from '@fluentui/scripts-tasks';
preset();"
`);
expect(justConfig).not.toContain(`task('build', 'build:react-components').cached?.();`);

await generator(tree, options);

justConfig = getJustConfig(projectConfig);

expect(justConfig).toMatchInlineSnapshot(`
"import { preset, task } from '@fluentui/scripts-tasks';
preset();
task('build', 'build:react-components').cached?.();"
`);
expect(justConfig).toContain(`task('build', 'build:react-components').cached?.();`);
});
});

Expand Down Expand Up @@ -1570,10 +1560,7 @@ function setupDummyPackage(
};

// this is needed to stop TS parsing static imports and evaluating them in nx dep graph tree as true dependency - https://github.com/nrwl/nx/issues/8938
const jestConfigTemplate = fs.readFileSync(
path.join(__dirname, '__fixtures__', 'old-jest-config.js__tmpl__'),
'utf-8',
);
const jestConfigTemplate = getFixture('old-jest-config.js__tmpl__');

const templates = {
packageJson: {
Expand Down Expand Up @@ -1641,11 +1628,7 @@ function setupDummyPackage(
babelConfig: {
...normalizedOptions.babelConfig,
},
justConfig: stripIndents`
import { preset } from '@fluentui/scripts-tasks';
preset();
`,
justConfig: getFixture('just-config.ts__tmpl__'),
};

tree.write(`${paths.root}/package.json`, serializeJson(templates.packageJson));
Expand Down Expand Up @@ -1680,7 +1663,7 @@ function setupDummyPackage(

function addConformanceSetup(tree: Tree, projectConfig: ReadProjectConfiguration) {
// this is needed to stop TS parsing static imports and evaluating them in nx dep graph tree as true dependency - https://github.com/nrwl/nx/issues/8938
const template = fs.readFileSync(path.join(__dirname, '__fixtures__', 'conformance-setup.ts__tmpl__'), 'utf-8');
const template = getFixture('conformance-setup.ts__tmpl__');
tree.write(`${projectConfig.root}/src/testing/isConformant.ts`, stripIndents`${template}`);
}

Expand Down Expand Up @@ -1725,3 +1708,12 @@ function append(tree: Tree, filePath: string, content: string) {
function getNormalizedPkgName(options: { pkgName: string; workspaceConfig: WorkspaceConfiguration }) {
return options.pkgName.replace(`@${options.workspaceConfig.npmScope}/`, '');
}

/**
*
* @param src - relative path/file name within `__fixtures__` folder
* @returns
*/
function getFixture(src: string) {
return fs.readFileSync(path.join(__dirname, '__fixtures__', src), 'utf-8');
}
9 changes: 3 additions & 6 deletions tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as path from 'path';
import * as os from 'os';
import * as ts from 'typescript';

import { getTemplate } from './lib/utils';
import { PackageJson, TsConfig } from '../../types';
import {
arePromptsEnabled,
Expand Down Expand Up @@ -435,12 +436,8 @@ const templates = {
sourceMaps: true,
};
},
justConfig: stripIndents`
import { preset, task } from '@fluentui/scripts-tasks';
preset();
task('build', 'build:react-components').cached?.();`,
// why not inline template ? this is needed to stop TS parsing static imports and evaluating them in nx dep graph tree as true dependency - https://github.com/nrwl/nx/issues/8938
justConfig: getTemplate(joinPathFragments(__dirname, 'files/just-config.ts__tmpl__'), {}),
};

function normalizeOptions(host: Tree, options: AssertedSchema) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function hello(){ return 'hello <%= greeting %>'; };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function hello(){ return 'world'; };
24 changes: 24 additions & 0 deletions tools/generators/migrate-converged-pkg/lib/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { stripIndents } from '@nrwl/devkit';
import path = require('path/posix');
import { getTemplate } from './utils';
describe(`utils`, () => {
describe(`#getTemplate`, () => {
const getFixturePath = (src: string) => {
return path.join(__dirname, '__fixtures__', src);
};

it(`should return template contents`, () => {
const expected = stripIndents`export function hello(){ return 'world'; };`;
const actual = getTemplate(getFixturePath('utils-hello.ts__tmpl__'), {});

expect(actual).toBe(expected);
});

it(`should return template contents and substitue variables`, () => {
const expected = stripIndents`export function hello(){ return 'hello champion!'; };`;
const actual = getTemplate(getFixturePath('utils-hello-substitute.ts__tmpl__'), { greeting: 'champion!' });

expect(actual).toBe(expected);
});
});
});
30 changes: 30 additions & 0 deletions tools/generators/migrate-converged-pkg/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { logger } from '@nrwl/devkit';
import * as ejs from 'ejs';
import * as fs from 'fs';
/**
* Similar to @nrwl/devkit#generateFiles function but for getting content only
*
* @param src - the source folder of files (absolute path)
* @param substitutions - an object of key-value pairs
* @returns
*/
export function getTemplate(src: string, substitutions: Record<string, unknown>) {
if (!fs.existsSync(src)) {
throw new Error(`${src} doesn't exists`);
}

const template = fs.readFileSync(src, 'utf8').trim();

if (!substitutions) {
return template;
}

try {
const content = ejs.render(template, substitutions, {});

return content;
} catch (err) {
logger.error(`Error in ${src}:`);
throw err;
}
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5262,6 +5262,11 @@
resolved "https://registry.yarnpkg.com/@types/doctrine/-/doctrine-0.0.5.tgz#9768264221e8fd422e4989c8cfdab2ff781f4eb6"
integrity sha512-JJwEeFy8Sl9ctiugU4h4DGN9hCB47oyhUkM2H8g8xZr4tHTEXtmV4U6krKrU8Ng0S7RlG/J7fkta1rGu3pq+YQ==

"@types/[email protected]":
version "3.1.2"
resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.1.2.tgz#75d277b030bc11b3be38c807e10071f45ebc78d9"
integrity sha512-ZmiaE3wglXVWBM9fyVC17aGPkLo/UgaOjEiI2FXQfyczrCefORPxIe+2dVmnmk3zkVIbizjrlQzmPGhSYGXG5g==

"@types/[email protected]":
version "3.10.7"
resolved "https://registry.yarnpkg.com/@types/enzyme/-/enzyme-3.10.7.tgz#ebdf3b972d293095e09af479e36c772025285e3a"
Expand Down Expand Up @@ -11630,7 +11635,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

ejs@^3.1.7:
ejs@3.1.9, ejs@^3.1.7:
version "3.1.9"
resolved "https://registry.yarnpkg.com/ejs/-/ejs-3.1.9.tgz#03c9e8777fe12686a9effcef22303ca3d8eeb361"
integrity sha512-rC+QVNMJWv+MtPgkt0y+0rVEIdbtxVADApW9JXrUVlzHetgcyczP/E7DJmWJ4fJCZF2cPcBk0laWO9ZHMG3DmQ==
Expand Down

0 comments on commit acb4830

Please sign in to comment.