Skip to content

Commit

Permalink
feat(scripts-generators): incorporate base all generation to create-p…
Browse files Browse the repository at this point in the history
…ackage and validate integrity on CI
  • Loading branch information
Hotell committed Apr 11, 2023
1 parent c7df259 commit 3e5cee2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
4 changes: 4 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
yarn tsc -p ./tsconfig.json
displayName: Type-check just.config.ts files
- script: |
yarn ts-node --swc scripts/generators/generate-ts-base-all-json.ts --verify
displayName: Check tsconfig.base.all.json integrity
- script: |
yarn check:installed-dependencies-versions
displayName: 'check packages: installed dependencies versions'
Expand Down
7 changes: 4 additions & 3 deletions scripts/generators/create-package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { spawnSync } from 'child_process';
import * as path from 'path';

import { PackageJson, findGitRoot } from '@fluentui/scripts-monorepo';
import { createPathAliasesConfig } from '@fluentui/scripts-storybook';
import { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import chalk from 'chalk';
import * as fs from 'fs-extra';
Expand All @@ -11,6 +10,8 @@ import _ from 'lodash';
import { Actions } from 'node-plop';
import { AddManyActionConfig, NodePlopAPI } from 'plop';

import { main as generateTsBaseAllJson } from '../generate-ts-base-all-json';

const root = findGitRoot();

const v8ReferencePackages = {
Expand Down Expand Up @@ -161,6 +162,8 @@ module.exports = (plop: NodePlopAPI) => {
if (migrateResult.status !== 0) {
throw new Error('Something went wrong running the migration. Please check previous logs for details.');
}

generateTsBaseAllJson();
return 'Successfully migrated package';
},
() => {
Expand Down Expand Up @@ -302,8 +305,6 @@ function updateNxWorkspace(_answers: Answers, config: { root: string; projectNam
const updatedNxWorkspace = jju.update(nxWorkspaceContent, nxWorkspace, { mode: 'json', indent: 2 });

fs.writeFileSync(paths.workspace, updatedNxWorkspace, 'utf-8');

createPathAliasesConfig({ relativeFolderPathFromRoot: '.' });
}

function getProjectMetadata(options: { root: string; name: string }) {
Expand Down
33 changes: 33 additions & 0 deletions scripts/generators/generate-ts-base-all-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createPathAliasesConfig } from '@fluentui/scripts-storybook';
import { isEqual } from 'lodash';
import * as yargs from 'yargs';

const isExecutedFromCli = require.main === module;

if (isExecutedFromCli) {
const argv = yargs
.option('verify', {
describe: 'Run check if ts base all is up to date. Used mostly on CI',
type: 'boolean',
})
.help().argv;

main(argv);
}

export function main(options?: yargs.Arguments<{ verify?: boolean }>) {
const { verify = false } = options ?? {};

const { mergedTsConfig, existingTsConfig, tsConfigAllFileName } = createPathAliasesConfig({
relativeFolderPathFromRoot: '.',
writeFileToDisk: verify === false,
});

if (verify && !isEqual(existingTsConfig, mergedTsConfig)) {
throw new Error(`
🚨 ${tsConfigAllFileName} is out of date.
Please update it by running 'yarn ts-node --swc scripts/generators/generate-ts-base-all-json.ts'.
`);
}
}
37 changes: 19 additions & 18 deletions scripts/storybook/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,37 +359,34 @@ function overrideDefaultBabelLoader(options) {
*
* Main purpose of this is to be used for build-less DX in webpack in tandem with {@link registerTsPaths}
*
* @param {{relativeFolderPathFromRoot?:string}} options
* @param {{relativeFolderPathFromRoot?:string,writeFileToDisk?:boolean}} options
*/
function createPathAliasesConfig(options = {}) {
const { relativeFolderPathFromRoot = './dist', writeFileToDisk = true } = options;
const rootPath = workspaceRoot;
const { relativeFolderPathFromRoot = './dist' } = options;
const mergedTsConfig = createMergedTsConfig({ rootPath });
const tsConfigAllPath = path.join(rootPath, relativeFolderPathFromRoot, 'tsconfig.base.all.json');
const mergedTsConfigRoot = path.join(rootPath, relativeFolderPathFromRoot);
const tsConfigAllFileName = 'tsconfig.base.all.json';
const tsConfigAllPath = path.join(mergedTsConfigRoot, tsConfigAllFileName);
const existingTsConfig = readJsonFile(tsConfigAllPath);

writeJsonFile(tsConfigAllPath, mergedTsConfig);

return { tsConfigAllPath, mergedTsConfig };
}

/**
*
* @param {{rootPath:string}} options
* @returns
*/
function createMergedTsConfig(options) {
const { rootPath } = options;
const baseConfigs = {
v0: readJsonFile(path.join(rootPath, 'tsconfig.base.v0.json')),
v8: readJsonFile(path.join(rootPath, 'tsconfig.base.v8.json')),
v9: readJsonFile(path.join(rootPath, 'tsconfig.base.json')),
};
const tsConfigBase = rootPath === mergedTsConfigRoot ? '.' : rootPath;
const mergedTsConfig = {
compilerOptions: {
moduleResolution: 'node',
forceConsistentCasingInFileNames: true,
skipLibCheck: true,
baseUrl: workspaceRoot,
typeRoots: ['node_modules/@types', './typings'],
isolatedModules: true,
preserveConstEnums: true,
sourceMap: true,
pretty: true,
rootDir: tsConfigBase,
baseUrl: tsConfigBase,
paths: {
...baseConfigs.v0.compilerOptions.paths,
...baseConfigs.v8.compilerOptions.paths,
Expand All @@ -398,7 +395,11 @@ function createMergedTsConfig(options) {
},
};

return mergedTsConfig;
if (writeFileToDisk) {
writeJsonFile(tsConfigAllPath, mergedTsConfig);
}

return { tsConfigAllPath, tsConfigAllFileName, mergedTsConfig, existingTsConfig };
}

exports.getPackageStoriesGlob = getPackageStoriesGlob;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.all.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"compilerOptions": {
"moduleResolution": "node",
"pretty": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"typeRoots": ["node_modules/@types", "./typings"],
"isolatedModules": true,
"preserveConstEnums": true,
"sourceMap": true,
"pretty": true,
"rootDir": ".",
"baseUrl": ".",
"paths": {
Expand Down

0 comments on commit 3e5cee2

Please sign in to comment.