@vamship/build-utils 1.0.0-0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const _gulp = require('gulp');
4
+ const _execa = require('execa');
5
+ const _path = require('path');
6
+ const _fs = require('fs');
7
+
8
+ const { createNpmPackageTask } = require('./utils');
9
+
10
+ /**
11
+ * Sub builder that packages the types exported by a project using npm pack. A
12
+ * custom package.json file is generated for the types.
13
+ *
14
+ * @private
15
+ * @param {Object} project Reference to an object that contains project metadata
16
+ * that can be used to customize build outputs.
17
+ * @param {Object} options An options object that can be used to customize the
18
+ * task.
19
+ *
20
+ * @returns {Function} A gulp task.
21
+ */
22
+ module.exports = (project, options) => {
23
+ const { name, snakeCasedName, version, license, keywords, rootDir } =
24
+ project;
25
+
26
+ const packageName = `${snakeCasedName}-types-${version}.tgz`;
27
+ const packageDir = rootDir
28
+ .getChild('working')
29
+ .getChild(project.exportedTypes);
30
+ const distDir = rootDir.getChild('dist');
31
+
32
+ const createPackageJsonTask = (cb) =>
33
+ _fs.writeFile(
34
+ packageDir.getFilePath('package.json'),
35
+ JSON.stringify({
36
+ name: `${name}-types`,
37
+ version,
38
+ description: `Types for project ${name}`,
39
+ license,
40
+ keywords,
41
+ }),
42
+ cb
43
+ );
44
+ createPackageJsonTask.displayName = 'package-types-packagejson';
45
+ createPackageJsonTask.description =
46
+ 'Creates a package.json file for the types';
47
+
48
+ const packTask = createNpmPackageTask(packageDir, packageName, distDir);
49
+ packTask.displayName = 'package-types-npm';
50
+ packTask.description =
51
+ 'Creates an NPM package for the types exported by the project';
52
+
53
+ return _gulp.series([createPackageJsonTask, packTask]);
54
+ };
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const _delete = require('delete');
4
+ const _execa = require('execa');
5
+ const _gulp = require('gulp');
6
+
7
+ /**
8
+ * Utility function that initializes and returns an array of tasks for npm
9
+ * package/publish operations.
10
+ *
11
+ * @param {Directory} packageDir The directory in which the package will be
12
+ * created.
13
+ * @param {String} packageName The name of the package that is being created.
14
+ * @param {Directory} targetDir The directory to which the package will be
15
+ * deployed.
16
+ *
17
+ * @returns {Function} A gulp task that creates the package.
18
+ */
19
+ module.exports.createNpmPackageTask = function (
20
+ packageDir,
21
+ packageName,
22
+ targetDir
23
+ ) {
24
+ const npmBin = 'npm';
25
+ const args = ['pack'];
26
+ const packageFile = packageDir.getFileGlob(packageName);
27
+
28
+ const packTask = () =>
29
+ _execa(npmBin, args, {
30
+ stdio: 'inherit',
31
+ cwd: packageDir.absolutePath,
32
+ });
33
+
34
+ packTask.displayName = 'package-npm';
35
+ packTask.description = 'Create a project distribution using npm';
36
+
37
+ const copyTask = () =>
38
+ _gulp.src(packageFile).pipe(_gulp.dest(targetDir.absolutePath));
39
+
40
+ copyTask.displayName = 'package-npm-copy';
41
+ copyTask.description =
42
+ 'Copies the project package to the distribution directory';
43
+
44
+ const deleteTask = () => _delete(packageFile);
45
+ deleteTask.displayName = 'package-npm-delete';
46
+ deleteTask.description =
47
+ 'Deletes the original packge, leaving just the one in the distribution directory';
48
+
49
+ return _gulp.series([packTask, copyTask, deleteTask]);
50
+ };
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const _gulp = require('gulp');
4
+
5
+ /**
6
+ * Builder function that can be used to generate a gulp task to publish a
7
+ * package. The task takes on different implementations based on project types.
8
+ * For example, javascript libraries will be published to an npm registry, using
9
+ * `npm publish`, while docker enabled projects will result in a docker image
10
+ * being published to a docker registry.
11
+ *
12
+ * @param {Object} project Reference to an object that contains project metadata
13
+ * that can be used to customize build outputs.
14
+ * @param {Object} options An options object that can be used to customize the
15
+ * task.
16
+ *
17
+ * @returns {Function} A gulp task.
18
+ */
19
+ module.exports = (project, options) => {
20
+ const { types } = options;
21
+ let createTask = null;
22
+
23
+ if (types) {
24
+ if (!project.hasExportedTypes) {
25
+ return;
26
+ }
27
+ createTask = require('./publish-types');
28
+ } else if (project.projectType === 'aws-microservice') {
29
+ createTask = require('./publish-aws');
30
+ } else if (project.hasDocker) {
31
+ createTask = require('./publish-docker');
32
+ } else if (project.projectType === 'lib' || project.projectType === 'cli') {
33
+ createTask = require('./publish-npm');
34
+ }
35
+
36
+ const task = createTask(project, options);
37
+
38
+ if (!(task instanceof Array)) {
39
+ if (types) {
40
+ task.displayName = 'publish-types';
41
+ task.description =
42
+ 'Publish the types exported by this project to a repository';
43
+ } else {
44
+ task.displayName = 'publish';
45
+ task.description = 'Publish the package to a repository';
46
+ }
47
+ }
48
+
49
+ return task;
50
+ };
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ const _gulp = require('gulp');
4
+ const _zip = require('gulp-zip');
5
+ const _execa = require('execa');
6
+
7
+ /**
8
+ * Sub builder that packages an aws-microservice project for deployment. This
9
+ * follows the build steps described for
10
+ * [lambda deployments](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html#nodejs-package-dependencies)
11
+ *
12
+ * @private
13
+ * @param {Object} project Reference to an object that contains project metadata
14
+ * that can be used to customize build outputs.
15
+ * @param {Object} options An options object that can be used to customize the
16
+ * task.
17
+ *
18
+ * @returns {Function} A gulp task.
19
+ */
20
+ module.exports = (project, options) => {
21
+ const { name, version, rootDir } = project;
22
+ const infraDir = rootDir.getChild('infra');
23
+ const workingDir = rootDir.getChild('working');
24
+
25
+ const packageName = `${name.replace(/\//g, '-')}-${version}.zip`;
26
+
27
+ const cdkBin = 'cdk';
28
+
29
+ const tasks = project.getCdkStacks().map((key) => {
30
+ const envFiles = [
31
+ infraDir.getFileGlob(`.env.${process.env.INFRA_ENV}`),
32
+ infraDir.getFileGlob('.env'),
33
+ ];
34
+
35
+ const args = [
36
+ 'deploy',
37
+ project.getCdkStackName(key),
38
+ '--app',
39
+ `"node ${workingDir.getFileGlob('infra/index')}"`,
40
+ ];
41
+
42
+ if (process.env.INFRA_NO_PROMPT === 'true') {
43
+ args.splice(1, 0, '--require-approval=never');
44
+ }
45
+
46
+ const task = () => {
47
+ project.initEnv(envFiles);
48
+ project.validateRequiredEnv();
49
+
50
+ return _execa(cdkBin, args, {
51
+ stdio: 'inherit',
52
+ });
53
+ };
54
+
55
+ task.displayName = `publish-${key}`;
56
+ task.description = `Publishes the ${key} stack to AWS using CDK`;
57
+
58
+ return task;
59
+ });
60
+
61
+ return tasks;
62
+ };
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const _execa = require('execa');
4
+ const _gulp = require('gulp');
5
+ const _log = require('fancy-log');
6
+ const _semver = require('semver');
7
+
8
+ /**
9
+ * Sub builder that builds a docker image based on a predefined dockerfile.
10
+ *
11
+ * @private
12
+ * @param {Object} project Reference to an object that contains project metadata
13
+ * that can be used to customize build outputs.
14
+ * @param {Object} options An options object that can be used to customize the
15
+ * task.
16
+ *
17
+ * @returns {Function} A gulp task.
18
+ */
19
+ module.exports = (project, options) => {
20
+ const { version } = project;
21
+
22
+ const dockerBin = 'docker';
23
+ const major = _semver.major(version);
24
+ const minor = _semver.minor(version);
25
+
26
+ const tasks = project.getDockerTargets().map((target) => {
27
+ const { name, isDefault, isDeprecated } = target;
28
+ const { latestOnly } = options;
29
+
30
+ let repo = target.repo;
31
+ if (typeof process.env.BUILD_DOCKER_REPO !== 'undefined') {
32
+ repo = process.env.BUILD_DOCKER_REPO;
33
+ _log.warn(`Docker repo override specified: [${repo}]`);
34
+ }
35
+
36
+ let suffix = `${isDefault ? '' : '-name'}${
37
+ latestOnly ? '-latest' : ''
38
+ }`;
39
+
40
+ const tagList = latestOnly
41
+ ? ['latest']
42
+ : [version, major, `${major}.${minor}`];
43
+
44
+ const tags = tagList.map((tag) => `${repo}:${tag}`);
45
+
46
+ const tasks = tags.map((tag) => {
47
+ const tagTask = () =>
48
+ _execa(dockerBin, ['tag', tag], {
49
+ stdio: 'inherit',
50
+ });
51
+ tagTask.displayName = `publish-docker-tag-${tag}${suffix}`;
52
+ tagTask.description = `Tag image with ${tag} (${name})`;
53
+
54
+ const pushTask = () =>
55
+ _execa(dockerBin, ['push', tag], {
56
+ stdio: 'inherit',
57
+ });
58
+ pushTask.displayName = `publish-docker-push-${tag}${suffix}`;
59
+ pushTask.description = `Publish docker image with ${tag} (${name}) to registry`;
60
+
61
+ const task = _gulp.series([tagTask, pushTask]);
62
+ task.displayName = `publish-docker-${tag}${suffix}`;
63
+ task.description = `Tag and push image with ${tag} (${name})`;
64
+
65
+ return task;
66
+ });
67
+
68
+ const task = _gulp.parallel(tasks);
69
+ task.displayName = `publish${suffix}`;
70
+ task.description = latestOnly
71
+ ? `Tags image ${name} with latest tag and pushes to registry`
72
+ : `Tags image ${name} with version tags, and pushes to registry`;
73
+
74
+ return task;
75
+ });
76
+
77
+ return tasks;
78
+ };
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const _gulp = require('gulp');
4
+ const _execa = require('execa');
5
+
6
+ /**
7
+ * Sub builder that publishes a project using npm publish.
8
+ *
9
+ * @private
10
+ * @param {Object} project Reference to an object that contains project metadata
11
+ * that can be used to customize build outputs.
12
+ * @param {Object} options An options object that can be used to customize the
13
+ * task.
14
+ *
15
+ * @returns {Function} A gulp task.
16
+ */
17
+ module.exports = (project, options) => {
18
+ const { snakeCasedName, version, rootDir } = project;
19
+
20
+ const packageName = `${snakeCasedName}-${version}.tgz`;
21
+
22
+ const npmBin = 'npm';
23
+ const args = ['publish', packageName];
24
+
25
+ const publishTask = () =>
26
+ _execa(npmBin, args, {
27
+ stdio: 'inherit',
28
+ cwd: rootDir.getChild('dist').absolutePath,
29
+ });
30
+
31
+ publishTask.displayName = 'publish-npm';
32
+ publishTask.description =
33
+ 'Publish an existing package to an npm repository';
34
+
35
+ return _gulp.parallel(publishTask);
36
+ };
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const _gulp = require('gulp');
4
+ const _execa = require('execa');
5
+
6
+ /**
7
+ * Sub builder that publishes a project using npm publish.
8
+ *
9
+ * @private
10
+ * @param {Object} project Reference to an object that contains project metadata
11
+ * that can be used to customize build outputs.
12
+ * @param {Object} options An options object that can be used to customize the
13
+ * task.
14
+ *
15
+ * @returns {Function} A gulp task.
16
+ */
17
+ module.exports = (project, options) => {
18
+ const { snakeCasedName, version, rootDir } = project;
19
+
20
+ const packageName = `${snakeCasedName}-types-${version}.tgz`;
21
+
22
+ const npmBin = 'npm';
23
+ const args = ['publish', packageName];
24
+
25
+ const publishTask = () =>
26
+ _execa(npmBin, args, {
27
+ stdio: 'inherit',
28
+ cwd: rootDir.getChild('dist').absolutePath,
29
+ });
30
+
31
+ publishTask.displayName = 'publish-npm-types';
32
+ publishTask.description =
33
+ 'Publish an existing package to an npm repository';
34
+
35
+ return _gulp.parallel([publishTask]);
36
+ };
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ const _gulp = require('gulp');
4
+ const _execa = require('execa');
5
+
6
+ /**
7
+ * Builder function that can be used to generate a gulp task to execute
8
+ * instrumented unit/api tests.
9
+ *
10
+ * @param {Object} project Reference to an object that contains project metadata
11
+ * that can be used to customize build outputs.
12
+ * @param {Object} options An options object that can be used to customize the
13
+ * task.
14
+ *
15
+ * @returns {Function} A gulp task.
16
+ */
17
+ module.exports = (project, options) => {
18
+ const { testType, watch } = Object.assign(
19
+ { testType: 'unit', watch: false },
20
+ options
21
+ );
22
+
23
+ if (testType === 'api' && project.projectType === 'lib') {
24
+ return;
25
+ }
26
+
27
+ const rootDir = project.jsRootDir;
28
+ const mochaBin = project.rootDir.getFilePath('node_modules/.bin/mocha');
29
+ const nycBin = project.rootDir.getFilePath('node_modules/.bin/nyc');
30
+
31
+ const paths = rootDir.getChild(`test/${testType}`).getAllFilesGlob('js');
32
+
33
+ const args = [
34
+ '--reporter',
35
+ 'text-summary',
36
+ '--reporter',
37
+ 'html',
38
+ '--temp-dir',
39
+ rootDir.getFilePath('coverage'),
40
+ ];
41
+
42
+ if (project.hasTypescript) {
43
+ ['--extension', '.ts'].map((arg) => args.push(arg));
44
+ }
45
+
46
+ [mochaBin, '--color', '-R', 'spec', '--recursive', paths].map((arg) =>
47
+ args.push(arg)
48
+ );
49
+
50
+ const task = () => _execa(nycBin, args, { stdio: 'inherit' });
51
+ task.displayName = `test-${testType}`;
52
+ task.description = `Execute ${testType} tests`;
53
+
54
+ if (watch) {
55
+ const watchPaths = ['src', 'test']
56
+ .map((dir) => rootDir.getChild(dir))
57
+ .map((dir) => dir.getAllFilesGlob('js'));
58
+
59
+ const watchTask = () => _gulp.watch(watchPaths, task);
60
+ watchTask.displayName = `watch-test-${testType}`;
61
+ watchTask.description = `Automatically execute ${testType} tests on change`;
62
+
63
+ return watchTask;
64
+ }
65
+
66
+ return task;
67
+ };