Boyscouting and refactoring

This commit is contained in:
Kai Röder 2019-06-02 14:59:16 +02:00
parent 69d9c98d3b
commit 3a92a9f79d
3 changed files with 54 additions and 46 deletions

View File

@ -1,31 +1,56 @@
import * as path from 'path';
import * as fs from 'fs';
import { getAngularAppTsConfigPath } from '../../lib/helpers';
import { readFileAsJson, writeFileAsJson } from '../../lib/helpers';
export function readFileAsJson(jsonPath) {
const filePath = path.resolve(jsonPath);
if (!fs.existsSync(filePath)) {
export function getAngularJson() {
const angularJsonPath = path.resolve('angular.json');
if (!fs.existsSync(angularJsonPath)) {
return false;
}
const jsonContent = fs.readFileSync(filePath, 'utf8');
const jsonContent = fs.readFileSync(angularJsonPath, 'utf8');
return JSON.parse(jsonContent);
}
export function writeFileAsJson(jsonPath, content) {
const filePath = path.resolve(jsonPath);
if (!fs.existsSync(filePath)) {
export function getAngularAppTsConfigPath() {
const angularJson = getAngularJson();
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 || !fs.existsSync(path.resolve(tsConfigPath))) {
return false;
}
const jsonContent = fs.readFileSync(filePath, 'utf8');
fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
return true;
const jsonContent = fs.readFileSync(tsConfigPath, 'utf8');
return JSON.parse(jsonContent);
}
export function writeAngularAppTsConfig(tsConfigJson) {
const content = `${JSON.stringify(tsConfigJson, null, 2)}\n`;
const tsConfigPath = getAngularAppTsConfigPath();
if (tsConfigPath) {
fs.writeFileSync(path.resolve(tsConfigPath), content, 'utf8');
}
}
function setStorybookTsconfigExtendsPath(tsconfigJson) {
const angularProjectTsConfigPath = getAngularAppTsConfigPath();
const newTsconfigJson = { ...tsconfigJson };
newTsconfigJson.extends = `../${angularProjectTsConfigPath}`;
return newTsconfigJson;
}
export function editStorybookTsConfig(tsconfigPath) {
const tsConfigJson = readFileAsJson(tsconfigPath);
const angularProjectTsConfigPath = getAngularAppTsConfigPath();
tsConfigJson.extends = `../${angularProjectTsConfigPath}`;
let tsConfigJson = readFileAsJson(tsconfigPath);
tsConfigJson = setStorybookTsconfigExtendsPath(tsConfigJson);
writeFileAsJson(tsconfigPath, tsConfigJson);
}

View File

@ -1,14 +1,16 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import { editStorybookTsConfig } from './angular-helpers';
import {
editStorybookTsConfig,
getAngularAppTsConfigJson,
writeAngularAppTsConfig,
} from './angular-helpers';
import {
getVersions,
getPackageJson,
writePackageJson,
getBabelDependencies,
installDependencies,
getAngularAppTsConfigJson,
writeAngularAppTsConfig,
} from '../../lib/helpers';
async function addDependencies(npmOptions) {

View File

@ -62,44 +62,25 @@ export function getBowerJson() {
return JSON.parse(jsonContent);
}
export function getAngularJson() {
const angularJsonPath = path.resolve('angular.json');
if (!fs.existsSync(angularJsonPath)) {
export function readFileAsJson(jsonPath) {
const filePath = path.resolve(jsonPath);
if (!fs.existsSync(filePath)) {
return false;
}
const jsonContent = fs.readFileSync(angularJsonPath, 'utf8');
const jsonContent = fs.readFileSync(filePath, 'utf8');
return JSON.parse(jsonContent);
}
export function getAngularAppTsConfigPath() {
const angularJson = getAngularJson();
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 || !fs.existsSync(path.resolve(tsConfigPath))) {
export function writeFileAsJson(jsonPath, content) {
const filePath = path.resolve(jsonPath);
if (!fs.existsSync(filePath)) {
return false;
}
const jsonContent = fs.readFileSync(tsConfigPath, 'utf8');
return JSON.parse(jsonContent);
}
export function writeAngularAppTsConfig(tsConfigJson) {
const content = `${JSON.stringify(tsConfigJson, null, 2)}\n`;
const tsConfigPath = getAngularAppTsConfigPath();
if (tsConfigPath) {
fs.writeFileSync(path.resolve(tsConfigPath), content, 'utf8');
}
const jsonContent = fs.readFileSync(filePath, 'utf8');
fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
return true;
}
export function writePackageJson(packageJson) {