@nx-dart/nx-dart
Version:
A Nx plugin, that adds support for developing Dart and Flutter packages in a Nx workspace
111 lines • 4.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const devkit_1 = require("@nrwl/devkit");
const path = require("path");
const dart_source_1 = require("../../utils/dart-source");
const package_1 = require("../../utils/package");
const generator_1 = require("../utils/generator");
const package_2 = require("../utils/package");
function normalizeOptions(tree, options) {
var _a;
const directory = (0, devkit_1.normalizePath)(options.directory);
const projectType = (_a = options.projectType) !== null && _a !== void 0 ? _a : (0, package_2.inferPackageProjectType)(tree, directory);
return {
directory,
projectType,
};
}
function default_1(tree, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const normalizedOptions = normalizeOptions(tree, options);
const existingProjects = Object.keys((0, devkit_1.getProjects)(tree));
const workspaceAnalysisOptions = (0, package_2.readAnalysisOptions)(tree);
const tasks = [];
tasks.push(addPackageProject(tree, existingProjects, workspaceAnalysisOptions, normalizedOptions.directory, normalizedOptions.projectType));
const exampleDir = `${normalizedOptions.directory}/example`;
if (tree.exists(`${exampleDir}/pubspec.yaml`)) {
tasks.push(addPackageProject(tree, existingProjects, workspaceAnalysisOptions, exampleDir, 'application'));
}
yield (0, devkit_1.formatFiles)(tree);
return (0, generator_1.runAllTasks)(tasks);
});
}
exports.default = default_1;
function addPackageProject(tree, existingProjects, workspaceAnalysisOptions, packageRoot, projectType) {
const pubspec = (0, package_2.readPubspec)(tree, packageRoot);
if (!pubspec) {
throw new Error(`Could not find a pubspec.yaml file in ${packageRoot}`);
}
const packageName = pubspec.name;
if (!packageName) {
throw new Error(`Could not find a name in ${(0, package_1.pubspecPath)(packageRoot)}`);
}
if (existingProjects.includes(packageName)) {
throw new Error(`A project with the name ${packageName} already exists.`);
}
(0, devkit_1.addProjectConfiguration)(tree, packageName, {
root: packageRoot,
projectType: projectType,
targets: buildPackageTargets(tree, packageRoot),
});
existingProjects.push(packageName);
if (workspaceAnalysisOptions !== undefined) {
// There are workspace wide analysis options, so we remove the package's
// analysis options.
return removeAnalysisOptions(tree, packageRoot);
}
}
function buildPackageTargets(tree, directory) {
const targets = {
format: {
executor: '@nx-dart/nx-dart:format',
outputs: [],
},
analyze: {
executor: '@nx-dart/nx-dart:analyze',
outputs: [],
},
};
const hasTests = tree.exists(`${directory}/test`);
const hasIntegrationTests = tree.exists(`${directory}/integration_test`);
if (hasTests) {
targets.test = {
executor: '@nx-dart/nx-dart:test',
outputs: [`${directory}/coverage`],
};
}
if (hasIntegrationTests) {
targets.e2e = {
executor: '@nx-dart/nx-dart:test',
outputs: [`${directory}/coverage`],
options: {
targets: ['integration_test'],
},
};
}
return targets;
}
function removeAnalysisOptions(tree, packageRoot) {
var _a;
const analysisOptions = (0, package_2.readAnalysisOptions)(tree, packageRoot);
if (analysisOptions !== undefined) {
if (analysisOptions.include) {
const include = path.join(packageRoot, analysisOptions.include);
if (include === 'analysis_options.yaml') {
// This file is including the analysis options from the workspace root.
// They customize the analysis options for the package, so we don't remove them.
return;
}
}
tree.delete(`${packageRoot}/analysis_options.yaml`);
const includePackage = (0, dart_source_1.packageNameFromUri)((_a = analysisOptions.include) !== null && _a !== void 0 ? _a : '');
if (includePackage) {
// Remove the package from which analysis options were included.
// The pubspec.yaml and analysis_options.yaml at the workspace root
// handle everything now.
return () => (0, package_1.removeDependencyFromPackage)(packageRoot, includePackage);
}
}
}
//# sourceMappingURL=generator.js.map