build-strap 4.0.0-dev.46 → 4.0.0-dev.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +3 -34
  2. package/package.json +23 -19
  3. package/src/ant.d.ts +19 -0
  4. package/src/ant.js +12 -36
  5. package/src/cert.d.ts +12 -0
  6. package/src/cert.js +87 -148
  7. package/src/clean.d.ts +4 -0
  8. package/src/clean.js +5 -13
  9. package/src/copy.d.ts +7 -0
  10. package/src/copy.js +48 -74
  11. package/src/cp.d.ts +17 -0
  12. package/src/cp.js +81 -165
  13. package/src/docker.container.d.ts +73 -0
  14. package/src/docker.container.js +208 -350
  15. package/src/docker.d.ts +68 -0
  16. package/src/docker.js +163 -339
  17. package/src/docker.network.d.ts +42 -0
  18. package/src/docker.network.js +67 -141
  19. package/src/docker.volume.d.ts +26 -0
  20. package/src/docker.volume.js +50 -124
  21. package/src/dockerCleanup.d.ts +38 -0
  22. package/src/dockerCleanup.js +62 -136
  23. package/src/dockerCompose.d.ts +154 -0
  24. package/src/dockerCompose.js +390 -716
  25. package/src/fs.d.ts +5 -0
  26. package/src/fs.js +33 -91
  27. package/src/git.d.ts +9 -0
  28. package/src/git.js +19 -29
  29. package/src/index.d.ts +20 -0
  30. package/src/index.js +20 -26
  31. package/src/maps.d.ts +33 -0
  32. package/src/maps.js +12 -82
  33. package/src/npm.d.ts +48 -0
  34. package/src/npm.js +128 -131
  35. package/src/pkg.d.ts +38 -0
  36. package/src/pkg.js +27 -74
  37. package/src/publish.d.ts +27 -0
  38. package/src/publish.js +51 -138
  39. package/src/run.d.ts +13 -0
  40. package/src/run.js +39 -84
  41. package/src/tgz.d.ts +13 -0
  42. package/src/tgz.js +54 -88
  43. package/src/version.d.ts +34 -0
  44. package/src/version.js +85 -126
  45. package/src/yarn.d.ts +44 -0
  46. package/src/yarn.js +147 -115
  47. package/ant.js +0 -43
  48. package/ant.js.flow +0 -42
  49. package/artifactory.js +0 -617
  50. package/artifactory.js.flow +0 -616
  51. package/cert.conf +0 -20
  52. package/cert.js +0 -153
  53. package/cert.js.flow +0 -152
  54. package/clean.js +0 -20
  55. package/clean.js.flow +0 -19
  56. package/copy.js +0 -80
  57. package/copy.js.flow +0 -79
  58. package/cp.js +0 -173
  59. package/cp.js.flow +0 -172
  60. package/docker.container.js +0 -390
  61. package/docker.container.js.flow +0 -389
  62. package/docker.js +0 -365
  63. package/docker.js.flow +0 -364
  64. package/docker.network.js +0 -160
  65. package/docker.network.js.flow +0 -159
  66. package/docker.volume.js +0 -136
  67. package/docker.volume.js.flow +0 -136
  68. package/dockerCleanup.js +0 -152
  69. package/dockerCleanup.js.flow +0 -151
  70. package/dockerCompose.js +0 -766
  71. package/dockerCompose.js.flow +0 -765
  72. package/flow.js +0 -21
  73. package/flow.js.flow +0 -20
  74. package/fs.js +0 -95
  75. package/fs.js.flow +0 -96
  76. package/git.js +0 -35
  77. package/git.js.flow +0 -34
  78. package/hg.js +0 -35
  79. package/hg.js.flow +0 -34
  80. package/index.js +0 -27
  81. package/index.js.flow +0 -26
  82. package/inline-resources.js +0 -158
  83. package/inline-resources.js.flow +0 -157
  84. package/maps.js +0 -112
  85. package/maps.js.flow +0 -111
  86. package/npm.js +0 -145
  87. package/npm.js.flow +0 -144
  88. package/pkg.js +0 -81
  89. package/pkg.js.flow +0 -80
  90. package/publish.js +0 -145
  91. package/publish.js.flow +0 -144
  92. package/run.js +0 -90
  93. package/run.js.flow +0 -89
  94. package/src/artifactory.js +0 -616
  95. package/src/cert.conf +0 -20
  96. package/src/flow.js +0 -20
  97. package/src/hg.js +0 -34
  98. package/src/inline-resources.js +0 -157
  99. package/src/test.js +0 -55
  100. package/test.js +0 -56
  101. package/test.js.flow +0 -55
  102. package/tgz.js +0 -102
  103. package/tgz.js.flow +0 -101
  104. package/version.js +0 -143
  105. package/version.js.flow +0 -142
  106. package/yarn.js +0 -124
  107. package/yarn.js.flow +0 -123
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # build-strap
2
- A suite of functions to help with writing build scripts using javascript code (which run on node).
2
+ A suite of functions to help with writing build scripts using TypeScript (or javascript) code (which run on node).
3
3
 
4
- This project builds itself, so take a look in the `tools/` folder for an example project. Run `./bs` to build.
4
+ This project builds itself, so take a look in the `targets/` folder for an example. Run `./bs` to build.
5
5
 
6
6
  [![CI Build](https://github.com/murrayju/build-strap/workflows/CI%20Build/badge.svg?branch=master)](https://github.com/murrayju/build-strap/actions?query=workflow%3A%22CI+Build%22)
7
7
  [![version](https://img.shields.io/github/v/tag/murrayju/build-strap.svg?label=version&sort=semver)](https://github.com/murrayju/build-strap/releases/latest)
@@ -41,39 +41,14 @@ By default, the build tools read project-specific configuration from your `packa
41
41
  "npm": {
42
42
  "publish": true,
43
43
  },
44
- "artifactory": {
45
- "root": "https://artifactory.your-company.com/artifactory/",
46
- "path": "com/your-company/your-project-name",
47
- "version-folders": false,
48
- "integration": "integration-repo",
49
- "integration-branch-folders": true,
50
- "integration-days-to-keep": 14,
51
- "integration-max-to-keep": 10,
52
- "release": "releases-repo",
53
- "release-branch": "master",
54
- "npm": "npm-repo",
55
- "npm-clean": "internal-npm-repo"
56
- },
57
44
  "docker": {
58
- "registry": "artifactory.your-company.com",
45
+ "registry": "ghcr.io",
59
46
  "repository": "your-company",
60
47
  "name": "your-project-name"
61
48
  }
62
49
  }
63
50
  }
64
51
  ```
65
- ### artifactory
66
- * **root**: base URL for the artifactory server
67
- * **path**: path to project root in the artifactory tree.
68
- * **version-folders**: if `true`, artifacts will be organized into subfolders for each version number
69
- * **integration**: name of the artifactory repo in which to place integration (not official release) artifacts.
70
- * **integration-branch-folders**: if `true`, artifacts will be organized into subfolders for each branch.
71
- * **integration-days-to-keep**: for artifact cleanup, specifies the max age in days before an artifact will be deleted.
72
- * **integration-max-to-keep**: for artifact cleanup, specifies the maximum number of artifacts to keep, regardless of age. Only the newest N artifacts will be kept.
73
- * **release**: name of the artifactory repo in which to place release artifacts.
74
- * **release-branch**: name of the mercurial branch from which official releases are built/published. Defaults to `master`.
75
- * **npm**: name of the artifactory repo in which to publish npm artifacts.
76
- * **npm-clean**: for artifact cleanup, the name of the artifactory repo used to find and delete old npm artifacts.
77
52
 
78
53
  ### docker
79
54
  * **registry**: base URL for the docker registry, as needed by `docker push`.
@@ -135,9 +110,3 @@ In order to publish to NPM, proper credentials must be provided to the script. B
135
110
  ```
136
111
  { "email": "builder@your-company.com", "username":"builder", "password":"abc123" }
137
112
  ```
138
-
139
- ## Artifactory Credentials
140
- In order to publish and/or delete things from Artifactory, proper credentials must be provided to the script. By default, these are read from the `ARTIFACTORY_CREDS` environment variable, but it is also possible to pass them as an argument to most functions. This is expected to be a JSON encoded string in the following format:
141
- ```
142
- { "email": "builder@your-company.com", "username":"builder", "password":"abc123" }
143
- ```
package/package.json CHANGED
@@ -1,31 +1,35 @@
1
1
  {
2
2
  "name": "build-strap",
3
- "version": "4.0.0-dev.46",
4
- "main": "index.js",
3
+ "version": "4.0.0-dev.54",
4
+ "description": "Node scripts for building things",
5
+ "license": "MIT",
6
+ "authors": [
7
+ "Justin Murray <justin@murrayju.com>"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/murrayju/build-strap"
12
+ },
13
+ "type": "module",
14
+ "main": "./src/index.js",
15
+ "types": "./src/index.d.ts",
16
+ "engines": {
17
+ "node": ">=16"
18
+ },
5
19
  "dependencies": {
6
- "bytes": "3",
20
+ "bytes": "^3.1.1",
7
21
  "chokidar": "^3.5.2",
8
22
  "cross-spawn": "^7.0.3",
9
23
  "fs-extra": "^10.0.0",
10
- "get-port": "^5.1.1",
11
- "glob": "^7.1.7",
12
- "is-reachable": "^5.0.0",
24
+ "get-port": "^6.0.0",
25
+ "glob": "^7.2.0",
26
+ "is-reachable": "^5.1.1",
13
27
  "js-yaml": "^4.1.0",
14
- "lodash": "^4.17.21",
15
- "md5-file": "^5.0.0",
28
+ "lodash-es": "^4.17.21",
16
29
  "moment": "^2.29.1",
17
- "request": "^2.88.2",
18
- "request-promise-native": "^1.0.9",
19
- "rimraf": "^3.0.2",
20
- "set-value": "^4.0.1",
21
- "source-map-support": "^0.5.16",
30
+ "node-fetch": "^3.1.0",
22
31
  "stream-counter": "1",
23
- "tar": "^6.1.9",
32
+ "tar": "^6.1.11",
24
33
  "url-join": "^4.0.1"
25
- },
26
- "peerDependencies": {},
27
- "engines": {
28
- "node": ">=7",
29
- "npm": ">=4"
30
34
  }
31
35
  }
package/src/ant.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { SpawnOptions } from './cp.js';
2
+ /**
3
+ * Named value pair to use when configuring an Ant build property
4
+ */
5
+ export interface AntPropertyConfiguration {
6
+ name: string;
7
+ value: string;
8
+ }
9
+ export interface AntSpawnOptions {
10
+ args?: string[];
11
+ properties?: AntPropertyConfiguration[];
12
+ spawnOptions?: SpawnOptions;
13
+ }
14
+ /**
15
+ * Executes the given ant command
16
+ * @param {string} target the ant target to invoke
17
+ * @param {AntSpawnOptions} options
18
+ */
19
+ export declare function ant(target: string, opts?: AntSpawnOptions): Promise<void>;
package/src/ant.js CHANGED
@@ -1,42 +1,18 @@
1
- // @flow
2
- import { spawn } from './cp';
3
- import type { SpawnOptions } from './cp';
4
-
5
- /**
6
- * Named value pair to use when configuring an Ant build property
7
- */
8
- export type AntPropertyConfiguration = {|
9
- name: string,
10
- value: string,
11
- |};
12
-
13
- export type AntSpawnOptions = {|
14
- properties?: AntPropertyConfiguration[], // Ant property values to configure
15
- args?: string[], // Any additional command line arguments to pass
16
- spawnOptions?: SpawnOptions, // Optional spawn configuration. Defaults to run the project root directory
17
- |};
18
-
1
+ import { spawn } from './cp.js';
19
2
  /**
20
3
  * Executes the given ant command
21
4
  * @param {string} target the ant target to invoke
22
5
  * @param {AntSpawnOptions} options
23
6
  */
24
- export async function ant(target: string, opts?: AntSpawnOptions) {
25
- const { properties = [], args = [], spawnOptions } = opts || {};
26
-
27
- const execOptions = {
28
- stdio: 'inherit',
29
- pipeOutput: true,
30
- ...spawnOptions,
31
- };
32
-
33
- await spawn(
34
- 'ant',
35
- [
36
- target,
37
- ...args,
38
- ...properties.map(({ name, value }) => `-D${name}=${value}`),
39
- ],
40
- execOptions,
41
- );
7
+ export async function ant(target, opts) {
8
+ const { properties = [], args = [], spawnOptions } = opts || {};
9
+ await spawn('ant', [
10
+ target,
11
+ ...args,
12
+ ...properties.map(({ name, value }) => `-D${name}=${value}`),
13
+ ], {
14
+ pipeOutput: true,
15
+ stdio: 'inherit',
16
+ ...spawnOptions,
17
+ });
42
18
  }
package/src/cert.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export interface CertConfig {
2
+ commonName?: string;
3
+ countryName?: string;
4
+ emailAddress?: string;
5
+ localityName?: string;
6
+ organizationName?: string;
7
+ organizationalUnitName?: string;
8
+ stateOrProvinceName?: string;
9
+ }
10
+ export declare const getCertConfig: () => CertConfig;
11
+ export declare const getIpAddresses: () => string[];
12
+ export declare function generateCert(targetDir: string, force?: boolean): Promise<void>;
package/src/cert.js CHANGED
@@ -1,152 +1,91 @@
1
- // @flow
1
+ import fs from 'fs-extra';
2
+ import fetch from 'node-fetch';
2
3
  import os from 'os';
3
- import rp from 'request-promise-native';
4
4
  import path from 'path';
5
- import fs from 'fs-extra';
6
- import { buildLog } from './run';
7
- import { readDir } from './fs';
8
- import { spawn } from './cp';
9
- import { getCfg } from './pkg';
10
-
11
- export type CertConfig = {
12
- countryName?: string,
13
- stateOrProvinceName?: string,
14
- localityName?: string,
15
- organizationName?: string,
16
- organizationalUnitName?: string,
17
- commonName?: string,
18
- emailAddress?: string,
19
- };
20
-
21
- export function getCertConfig(): CertConfig {
22
- return getCfg().cert || {};
23
- }
24
-
25
- export function getIpAddresses(): string[] {
26
- const iFaces = os.networkInterfaces();
27
- return Object.keys(iFaces).reduce(
28
- (prev, ifName) => [
29
- ...prev,
30
- ...iFaces[ifName].map((iFace) => iFace.address),
31
- ],
32
- [],
33
- );
34
- }
35
-
5
+ import { spawn } from './cp.js';
6
+ import { readDir } from './fs.js';
7
+ import { getCfg } from './pkg.js';
8
+ import { buildLog } from './run.js';
9
+ export const getCertConfig = () => getCfg().cert ?? {};
10
+ export const getIpAddresses = () => Object.values(os.networkInterfaces()).flatMap((iFaces) => iFaces?.map((iFace) => iFace.address) ?? []);
36
11
  // Generate a self-signed certificate (if it doesn't exist)
37
- export async function generateCert(
38
- targetDir: string,
39
- force: boolean = process.argv.includes('--force-generateCert'),
40
- ) {
41
- if (process.argv.includes('--no-generateCert')) {
42
- buildLog('Skipping due to --no-generateCert');
43
- return;
44
- }
45
- buildLog(`scanning ${targetDir} for certificates...`);
46
- if (
47
- !force &&
48
- (await readDir(`${targetDir}/server.@(crt|key)`)).length === 2
49
- ) {
50
- buildLog('Skipping certificate generation: certificate already exists');
51
- return;
52
- }
53
- buildLog('Generating self-signed certificate...');
54
- const {
55
- countryName = 'US',
56
- stateOrProvinceName = 'Pennsylvania',
57
- localityName = 'SomeCity',
58
- organizationName = 'Company',
59
- organizationalUnitName = 'Product',
60
- commonName = 'app.company.com',
61
- emailAddress = 'contact@company.com',
62
- } = getCertConfig();
63
- const hostname = os.hostname();
64
- buildLog(`hostname: ${hostname}`);
65
- const ipAddresses = getIpAddresses();
66
- buildLog(`ip addresses: ${JSON.stringify(ipAddresses)}`);
67
- const ifconfig = await rp.get('https://ifconfig.co', { json: true });
68
- const externalIp4 = ifconfig.ip;
69
- buildLog(`external ipv4: ${externalIp4}`);
70
- const dnsName = ifconfig.hostname;
71
- buildLog(`dnsName: ${dnsName}`);
72
-
73
- await fs.ensureDir(targetDir);
74
- const keyFile = path.resolve(targetDir, 'server.key');
75
- const csrFile = path.resolve(targetDir, 'server.csr');
76
- const crtFile = path.resolve(targetDir, 'server.crt');
77
- const confTemplateFile = path.resolve(__dirname, 'cert.conf');
78
- const confFile = path.resolve(targetDir, 'cert.conf.generated');
79
- const confTemplate = await fs.readFile(confTemplateFile, 'utf8');
80
- await fs.writeFile(
81
- confFile,
82
- confTemplate
83
- .replace('{{countryName}}', countryName)
84
- .replace('{{stateOrProvinceName}}', stateOrProvinceName)
85
- .replace('{{localityName}}', localityName)
86
- .replace('{{organizationName}}', organizationName)
87
- .replace('{{organizationalUnitName}}', organizationalUnitName)
88
- .replace('{{commonName}}', commonName)
89
- .replace('{{emailAddress}}', emailAddress)
90
- .replace(
91
- '{{SAN_IP}}',
92
- ipAddresses.reduce((acc, next) => `${acc},IP:${next}`, ''),
93
- )
94
- .replace('{{SAN_DNS}}', `,DNS:${hostname},DNS:${dnsName}`),
95
- 'utf8',
96
- );
97
-
98
- buildLog('generating key file...');
99
- await spawn(
100
- 'openssl',
101
- [
102
- 'genpkey',
103
- '-algorithm',
104
- 'RSA',
105
- '-pkeyopt',
106
- 'rsa_keygen_bits:2048',
107
- '-out',
108
- keyFile,
109
- ],
110
- { pipeOutput: true },
111
- );
112
- buildLog('generating csr file...');
113
- await spawn(
114
- 'openssl',
115
- [
116
- 'req',
117
- '-new',
118
- '-key',
119
- keyFile,
120
- '-out',
121
- csrFile,
122
- '-subj',
123
- `/C=${countryName}/ST=${stateOrProvinceName}/L=${localityName}/O=${organizationName}/OU=${organizationalUnitName}/CN=${commonName}/CN=${externalIp4}/CN=${hostname}/CN=${dnsName}${ipAddresses.reduce(
124
- (acc, next) => `${acc}/CN=${next}`,
125
- )}/CN=localhost`,
126
- '-config',
127
- confFile,
128
- ],
129
- { pipeOutput: true },
130
- );
131
- buildLog('generating cert...');
132
- await spawn(
133
- 'openssl',
134
- [
135
- 'x509',
136
- '-req',
137
- '-days',
138
- '365',
139
- '-in',
140
- csrFile,
141
- '-signkey',
142
- keyFile,
143
- '-out',
144
- crtFile,
145
- '-extfile',
146
- confFile,
147
- '-extensions',
148
- 'v3_req',
149
- ],
150
- { pipeOutput: true },
151
- );
12
+ export async function generateCert(targetDir, force = process.argv.includes('--force-generateCert')) {
13
+ if (process.argv.includes('--no-generateCert')) {
14
+ buildLog('Skipping due to --no-generateCert');
15
+ return;
16
+ }
17
+ buildLog(`scanning ${targetDir} for certificates...`);
18
+ if (!force &&
19
+ (await readDir(`${targetDir}/server.@(crt|key)`)).length === 2) {
20
+ buildLog('Skipping certificate generation: certificate already exists');
21
+ return;
22
+ }
23
+ buildLog('Generating self-signed certificate...');
24
+ const { commonName = 'app.company.com', countryName = 'US', emailAddress = 'contact@company.com', localityName = 'SomeCity', organizationName = 'Company', organizationalUnitName = 'Product', stateOrProvinceName = 'Pennsylvania', } = getCertConfig();
25
+ const hostname = os.hostname();
26
+ buildLog(`hostname: ${hostname}`);
27
+ const ipAddresses = getIpAddresses();
28
+ buildLog(`ip addresses: ${JSON.stringify(ipAddresses)}`);
29
+ const ifconfig = await fetch('https://ifconfig.co').then((r) => r.json());
30
+ const externalIp4 = ifconfig.ip;
31
+ buildLog(`external ipv4: ${externalIp4}`);
32
+ const dnsName = ifconfig.hostname;
33
+ buildLog(`dnsName: ${dnsName}`);
34
+ await fs.ensureDir(targetDir);
35
+ const keyFile = path.resolve(targetDir, 'server.key');
36
+ const csrFile = path.resolve(targetDir, 'server.csr');
37
+ const crtFile = path.resolve(targetDir, 'server.crt');
38
+ const confTemplateFile = path.resolve(__dirname, 'cert.conf');
39
+ const confFile = path.resolve(targetDir, 'cert.conf.generated');
40
+ const confTemplate = await fs.readFile(confTemplateFile, 'utf8');
41
+ await fs.writeFile(confFile, confTemplate
42
+ .replace('{{countryName}}', countryName)
43
+ .replace('{{stateOrProvinceName}}', stateOrProvinceName)
44
+ .replace('{{localityName}}', localityName)
45
+ .replace('{{organizationName}}', organizationName)
46
+ .replace('{{organizationalUnitName}}', organizationalUnitName)
47
+ .replace('{{commonName}}', commonName)
48
+ .replace('{{emailAddress}}', emailAddress)
49
+ .replace('{{SAN_IP}}', ipAddresses.reduce((acc, next) => `${acc},IP:${next}`, ''))
50
+ .replace('{{SAN_DNS}}', `,DNS:${hostname},DNS:${dnsName}`), 'utf8');
51
+ buildLog('generating key file...');
52
+ await spawn('openssl', [
53
+ 'genpkey',
54
+ '-algorithm',
55
+ 'RSA',
56
+ '-pkeyopt',
57
+ 'rsa_keygen_bits:2048',
58
+ '-out',
59
+ keyFile,
60
+ ], { pipeOutput: true });
61
+ buildLog('generating csr file...');
62
+ await spawn('openssl', [
63
+ 'req',
64
+ '-new',
65
+ '-key',
66
+ keyFile,
67
+ '-out',
68
+ csrFile,
69
+ '-subj',
70
+ `/C=${countryName}/ST=${stateOrProvinceName}/L=${localityName}/O=${organizationName}/OU=${organizationalUnitName}/CN=${commonName}/CN=${externalIp4}/CN=${hostname}/CN=${dnsName}${ipAddresses.reduce((acc, next) => `${acc}/CN=${next}`)}/CN=localhost`,
71
+ '-config',
72
+ confFile,
73
+ ], { pipeOutput: true });
74
+ buildLog('generating cert...');
75
+ await spawn('openssl', [
76
+ 'x509',
77
+ '-req',
78
+ '-days',
79
+ '365',
80
+ '-in',
81
+ csrFile,
82
+ '-signkey',
83
+ keyFile,
84
+ '-out',
85
+ crtFile,
86
+ '-extfile',
87
+ confFile,
88
+ '-extensions',
89
+ 'v3_req',
90
+ ], { pipeOutput: true });
152
91
  }
package/src/clean.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Cleans up the build output directories.
3
+ */
4
+ export declare function clean(globs?: string[]): Promise<void>;
package/src/clean.js CHANGED
@@ -1,19 +1,11 @@
1
- // @flow
2
- import { cleanDir } from './fs';
3
-
1
+ import { cleanDir } from './fs.js';
4
2
  /**
5
3
  * Cleans up the build output directories.
6
4
  */
7
- export async function clean(
8
- globs: string[] = ['dist/*', 'out/*', 'build/*'],
9
- ): Promise<void> {
10
- await Promise.all(
11
- globs.map((glob) =>
12
- cleanDir(glob, {
13
- nosort: true,
5
+ export async function clean(globs = ['dist/*', 'out/*', 'build/*']) {
6
+ await Promise.all(globs.map((glob) => cleanDir(glob, {
14
7
  dot: true,
15
8
  ignore: ['.git', '.hg'],
16
- }),
17
- ),
18
- );
9
+ nosort: true,
10
+ })));
19
11
  }
package/src/copy.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export interface CopySrcOptions {
2
+ cbFn?: () => void;
3
+ from: string;
4
+ to: string;
5
+ watch?: boolean;
6
+ }
7
+ export declare function copySrc({ from, to, watch, cbFn, }: CopySrcOptions): Promise<void>;
package/src/copy.js CHANGED
@@ -1,79 +1,53 @@
1
- // @flow
2
- import path from 'path';
3
1
  import chokidar from 'chokidar';
4
- import { copyFile, makeDir, copyDir, cleanDir } from './fs';
5
- import { onKillSignal } from './cp';
6
- import { buildLog } from './run';
7
- import { dependencies } from './inline-resources';
8
-
2
+ import fs from 'fs-extra';
3
+ import path from 'path';
4
+ import { onKillSignal } from './cp.js';
5
+ import { cleanDir, copyDir } from './fs.js';
6
+ import { buildLog } from './run.js';
9
7
  let timer = null;
10
8
  async function throttledCallback(cbFn) {
11
- if (!cbFn || typeof cbFn !== 'function') {
12
- return;
13
- }
14
- if (timer) {
15
- clearTimeout(timer);
16
- timer = null;
17
- }
18
- timer = setTimeout(() => {
19
- timer = null;
20
- cbFn();
21
- }, 200);
9
+ if (!cbFn || typeof cbFn !== 'function') {
10
+ return;
11
+ }
12
+ if (timer) {
13
+ clearTimeout(timer);
14
+ timer = null;
15
+ }
16
+ timer = setTimeout(() => {
17
+ timer = null;
18
+ cbFn();
19
+ }, 200);
22
20
  }
23
-
24
- export type CopySrcOptions = {
25
- from: string,
26
- to: string,
27
- watch?: boolean,
28
- cbFn?: () => Promise<any> | void,
29
- };
30
-
31
- export async function copySrc({
32
- from,
33
- to,
34
- watch = process.argv.includes('--watch'),
35
- cbFn,
36
- }: CopySrcOptions = {}): Promise<void> {
37
- await copyDir(from, to);
38
- if (cbFn) await cbFn();
39
- if (watch) {
40
- const watcher = chokidar.watch([path.join(from, '/**/*')], {
41
- ignoreInitial: true,
42
- });
43
-
44
- watcher.on('all', async (event, filePath) => {
45
- const start = new Date();
46
- const src = path.relative(from, filePath);
47
- const dest = path.join(to, src);
48
- switch (event) {
49
- case 'add':
50
- case 'change':
51
- await makeDir(path.dirname(dest));
52
- await copyFile(filePath, dest);
53
- break;
54
- case 'unlink':
55
- case 'unlinkDir':
56
- cleanDir(dest, { nosort: true, dot: true });
57
- break;
58
- default:
59
- return;
60
- }
61
- // Check dependencies
62
- if (dependencies[dest]) {
63
- await Promise.all(
64
- dependencies[dest].map(async (dep) => {
65
- const toCopy = path.join(from, path.relative(to, dep));
66
- await copyFile(toCopy, dep);
67
- buildLog(`copied '${dep}' due to changed dependency`);
68
- }),
69
- );
70
- }
71
- const end = new Date();
72
- const time = end.getTime() - start.getTime();
73
- buildLog(`${event} '${dest}' after ${time} ms`, end);
74
- throttledCallback(cbFn);
75
- });
76
-
77
- onKillSignal(() => watcher.close());
78
- }
21
+ export async function copySrc({ from, to, watch = process.argv.includes('--watch'), cbFn, }) {
22
+ await copyDir(from, to);
23
+ if (cbFn)
24
+ await cbFn();
25
+ if (watch) {
26
+ const watcher = chokidar.watch([path.join(from, '/**/*')], {
27
+ ignoreInitial: true,
28
+ });
29
+ watcher.on('all', async (event, filePath) => {
30
+ const start = new Date();
31
+ const src = path.relative(from, filePath);
32
+ const dest = path.join(to, src);
33
+ switch (event) {
34
+ case 'add':
35
+ case 'change':
36
+ await fs.ensureDir(path.dirname(dest));
37
+ await fs.copyFile(filePath, dest);
38
+ break;
39
+ case 'unlink':
40
+ case 'unlinkDir':
41
+ cleanDir(dest, { dot: true, nosort: true });
42
+ break;
43
+ default:
44
+ return;
45
+ }
46
+ const end = new Date();
47
+ const time = end.getTime() - start.getTime();
48
+ buildLog(`${event} '${dest}' after ${time} ms`, end);
49
+ throttledCallback(cbFn);
50
+ });
51
+ onKillSignal(() => watcher.close());
52
+ }
79
53
  }
package/src/cp.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ import cp from 'child_process';
3
+ export interface SpawnOptions extends cp.SpawnOptions {
4
+ captureOutput?: boolean;
5
+ pipeOutput?: boolean;
6
+ rejectOnErrorCode?: boolean;
7
+ }
8
+ export interface SpawnResult {
9
+ code: number;
10
+ output: string;
11
+ signal: string;
12
+ stderr: string;
13
+ stdout: string;
14
+ }
15
+ export declare function spawn(command: string, args?: string[], opts?: SpawnOptions | null): Promise<SpawnResult>;
16
+ export declare const exec: typeof cp.exec.__promisify__;
17
+ export declare const onKillSignal: (cbFn: () => void) => void;