@doku-dev/doku-fragment
Version:
A new Angular UI library that moving away from Bootstrap and built from scratch.
125 lines • 5.79 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addPackageToPackageJson = exports.getProjectStyleFile = exports.getProjectTargetOptions = exports.getPackageVersionFromPackageJson = exports.getActiveProject = void 0;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
// Regular expression that matches all files that have a proper stylesheet extension
const validStyleFileRegex = /\.(c|le|sc|sa)ss/;
/**
* Get project from workspace by name or get directly if single project.
* @param tree Host tree
* @param projectName Project name from schema
* @returns Project definition or undefined
*/
function getActiveProject(workspace, projectName) {
return __awaiter(this, void 0, void 0, function* () {
let project;
// Get workspace project by name.
if (!project && projectName) {
project = workspace.projects.get(projectName);
}
// If project by name does not exists, get directly in the workspace if it's a single project.
if (!project && workspace.projects.size === 1) {
project = Array.from(workspace.projects.values()).at(0);
}
return project;
});
}
exports.getActiveProject = getActiveProject;
/**
* Get package version by looking at the package.json in the given tree
*/
function getPackageVersionFromPackageJson(tree, name, includeDevDependencies = false) {
var _a, _b;
if (!tree.exists('package.json')) {
return null;
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const packageJson = JSON.parse(tree.read('package.json').toString('utf8'));
if ((_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a[name]) {
return packageJson.dependencies[name];
}
if (includeDevDependencies && ((_b = packageJson.devDependencies) === null || _b === void 0 ? void 0 : _b[name])) {
return packageJson.devDependencies[name];
}
return null;
}
exports.getPackageVersionFromPackageJson = getPackageVersionFromPackageJson;
/**
* Get options for the build target of the given project
*/
function getProjectTargetOptions(project, buildTarget) {
const buildTargetObject = project.targets.get(buildTarget);
if (buildTargetObject === null || buildTargetObject === void 0 ? void 0 : buildTargetObject.options) {
return buildTargetObject.options;
}
throw new schematics_1.SchematicsException(`Cannot determine project target configuration for: ${buildTarget}.`);
}
exports.getProjectTargetOptions = getProjectTargetOptions;
/**
* Gets a style file with the given extension in a project and returns its path. If no
* extension is specified, it will return null.
*/
function getProjectStyleFile(project) {
const buildOptions = getProjectTargetOptions(project, 'build');
if (buildOptions['styles'] &&
Array.isArray(buildOptions['styles']) &&
buildOptions['styles'].length) {
const styles = buildOptions['styles']
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.map((s) => (typeof s === 'string' ? s : s['input']))
.filter((value) => !!value);
// Look for the default style file that is generated for new projects by the Angular CLI. This
// default style file is usually called `styles.extension` unless it has been changed explicitly.
const defaultMainStylePath = styles.find((path) => {
const expectedPathWithoutExtension = `${project.sourceRoot}/styles`;
const pathMatched = path.startsWith(expectedPathWithoutExtension);
const validExtension = validStyleFileRegex.test(path);
return pathMatched && validExtension;
});
if (defaultMainStylePath) {
return (0, core_1.normalize)(defaultMainStylePath);
}
return null;
}
return null;
}
exports.getProjectStyleFile = getProjectStyleFile;
/**
* Adds a package to the package.json in the given tree
*/
function addPackageToPackageJson(tree, pkg, version, devDependency = false) {
if (tree.exists('package.json')) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const sourceText = tree.read('package.json').toString('utf-8');
const json = JSON.parse(sourceText);
const dependenciesType = devDependency ? 'devDependencies' : 'dependencies';
const dependencies = json[dependenciesType] || {};
if (!dependencies[pkg]) {
dependencies[pkg] = version;
json[dependenciesType] = sortObjectByKeys(dependencies);
}
tree.overwrite('package.json', JSON.stringify(json, null, 2));
}
return tree;
}
exports.addPackageToPackageJson = addPackageToPackageJson;
/**
* Sorts the keys of the given object.
* @returns A new object instance with sorted keys
*/
function sortObjectByKeys(obj) {
return Object.keys(obj)
.sort()
.reduce((result, key) => (result[key] = obj[key]) && result, {});
}
//# sourceMappingURL=util.js.map