mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-09 00:19:13 +08:00
Improve tsconfig.app.json detection for app/angular (#6940)
Improve tsconfig.app.json detection for app/angular
This commit is contained in:
commit
640d2ad84a
37
lib/cli/generators/ANGULAR/angular-helpers.js
vendored
Normal file
37
lib/cli/generators/ANGULAR/angular-helpers.js
vendored
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import * as path from 'path';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import { readFileAsJson, writeFileAsJson } from '../../lib/helpers';
|
||||||
|
|
||||||
|
export function getAngularAppTsConfigPath() {
|
||||||
|
const angularJson = readFileAsJson('angular.json');
|
||||||
|
const { defaultProject } = angularJson;
|
||||||
|
const tsConfigPath = angularJson.projects[defaultProject].architect.build.options.tsConfig;
|
||||||
|
|
||||||
|
if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return tsConfigPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAngularAppTsConfigJson() {
|
||||||
|
const tsConfigPath = getAngularAppTsConfigPath();
|
||||||
|
|
||||||
|
if (!tsConfigPath) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return readFileAsJson(tsConfigPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
function setStorybookTsconfigExtendsPath(tsconfigJson) {
|
||||||
|
const angularProjectTsConfigPath = getAngularAppTsConfigPath();
|
||||||
|
const newTsconfigJson = { ...tsconfigJson };
|
||||||
|
newTsconfigJson.extends = `../${angularProjectTsConfigPath}`;
|
||||||
|
return newTsconfigJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function editStorybookTsConfig(tsconfigPath) {
|
||||||
|
let tsConfigJson = readFileAsJson(tsconfigPath);
|
||||||
|
tsConfigJson = setStorybookTsconfigExtendsPath(tsConfigJson);
|
||||||
|
writeFileAsJson(tsconfigPath, tsConfigJson);
|
||||||
|
}
|
@ -1,13 +1,17 @@
|
|||||||
import mergeDirs from 'merge-dirs';
|
import mergeDirs from 'merge-dirs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import {
|
||||||
|
editStorybookTsConfig,
|
||||||
|
getAngularAppTsConfigJson,
|
||||||
|
getAngularAppTsConfigPath,
|
||||||
|
} from './angular-helpers';
|
||||||
import {
|
import {
|
||||||
getVersions,
|
getVersions,
|
||||||
getPackageJson,
|
getPackageJson,
|
||||||
writePackageJson,
|
writePackageJson,
|
||||||
getBabelDependencies,
|
getBabelDependencies,
|
||||||
installDependencies,
|
installDependencies,
|
||||||
getAngularAppTsConfigJson,
|
writeFileAsJson,
|
||||||
writeAngularAppTsConfig,
|
|
||||||
} from '../../lib/helpers';
|
} from '../../lib/helpers';
|
||||||
|
|
||||||
async function addDependencies(npmOptions) {
|
async function addDependencies(npmOptions) {
|
||||||
@ -62,7 +66,7 @@ function editAngularAppTsConfig() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tsConfigJson.exclude = [...exclude, glob];
|
tsConfigJson.exclude = [...exclude, glob];
|
||||||
writeAngularAppTsConfig(tsConfigJson);
|
writeFileAsJson(getAngularAppTsConfigPath(), tsConfigJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async npmOptions => {
|
export default async npmOptions => {
|
||||||
@ -70,4 +74,5 @@ export default async npmOptions => {
|
|||||||
|
|
||||||
await addDependencies(npmOptions);
|
await addDependencies(npmOptions);
|
||||||
editAngularAppTsConfig();
|
editAngularAppTsConfig();
|
||||||
|
editStorybookTsConfig(path.resolve('./.storybook/tsconfig.json'));
|
||||||
};
|
};
|
||||||
|
@ -1,20 +1,9 @@
|
|||||||
{
|
{
|
||||||
"extends": "../src/tsconfig.app.json",
|
"extends": "%SET_DURING_SB_INIT%",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"types": [
|
"types": ["node"]
|
||||||
"node"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": ["../src/test.ts", "../src/**/*.spec.ts", "../projects/**/*.spec.ts"],
|
||||||
"../src/test.ts",
|
"include": ["../src/**/*", "../projects/**/*"],
|
||||||
"../src/**/*.spec.ts",
|
"files": ["./typings.d.ts"]
|
||||||
"../projects/**/*.spec.ts"
|
|
||||||
],
|
|
||||||
"include": [
|
|
||||||
"../src/**/*",
|
|
||||||
"../projects/**/*"
|
|
||||||
],
|
|
||||||
"files": [
|
|
||||||
"./typings.d.ts"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
@ -62,44 +62,25 @@ export function getBowerJson() {
|
|||||||
return JSON.parse(jsonContent);
|
return JSON.parse(jsonContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAngularJson() {
|
export function readFileAsJson(jsonPath) {
|
||||||
const angularJsonPath = path.resolve('angular.json');
|
const filePath = path.resolve(jsonPath);
|
||||||
if (!fs.existsSync(angularJsonPath)) {
|
if (!fs.existsSync(filePath)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonContent = fs.readFileSync(angularJsonPath, 'utf8');
|
const jsonContent = fs.readFileSync(filePath, 'utf8');
|
||||||
return JSON.parse(jsonContent);
|
return JSON.parse(jsonContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAngularAppTsConfigPath() {
|
export function writeFileAsJson(jsonPath, content) {
|
||||||
const angularJson = getAngularJson();
|
const filePath = path.resolve(jsonPath);
|
||||||
const { defaultProject } = angularJson;
|
if (!fs.existsSync(filePath)) {
|
||||||
const tsConfigPath = angularJson.projects[defaultProject].architect.build.options.tsConfig;
|
|
||||||
|
|
||||||
if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return tsConfigPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getAngularAppTsConfigJson() {
|
|
||||||
const tsConfigPath = getAngularAppTsConfigPath();
|
|
||||||
|
|
||||||
if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonContent = fs.readFileSync(tsConfigPath, 'utf8');
|
const jsonContent = fs.readFileSync(filePath, 'utf8');
|
||||||
return JSON.parse(jsonContent);
|
fs.writeFileSync(filePath, `${JSON.stringify(content, null, 2)}\n`);
|
||||||
}
|
return true;
|
||||||
|
|
||||||
export function writeAngularAppTsConfig(tsConfigJson) {
|
|
||||||
const content = `${JSON.stringify(tsConfigJson, null, 2)}\n`;
|
|
||||||
const tsConfigPath = getAngularAppTsConfigPath();
|
|
||||||
if (tsConfigPath) {
|
|
||||||
fs.writeFileSync(path.resolve(tsConfigPath), content, 'utf8');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writePackageJson(packageJson) {
|
export function writePackageJson(packageJson) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user