build-strap 4.0.0-dev.46 → 4.0.0

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 (129) hide show
  1. package/README.md +3 -34
  2. package/dist/src/ant.d.ts +19 -0
  3. package/dist/src/ant.js +18 -0
  4. package/dist/src/cert.d.ts +12 -0
  5. package/dist/src/cert.js +91 -0
  6. package/dist/src/clean.d.ts +4 -0
  7. package/dist/src/clean.js +11 -0
  8. package/dist/src/copy.d.ts +7 -0
  9. package/dist/src/copy.js +53 -0
  10. package/dist/src/cp.d.ts +17 -0
  11. package/dist/src/cp.js +88 -0
  12. package/dist/src/docker.container.d.ts +73 -0
  13. package/dist/src/docker.container.js +247 -0
  14. package/dist/src/docker.d.ts +68 -0
  15. package/dist/src/docker.js +188 -0
  16. package/dist/src/docker.network.d.ts +42 -0
  17. package/dist/src/docker.network.js +85 -0
  18. package/dist/src/docker.volume.d.ts +26 -0
  19. package/dist/src/docker.volume.js +62 -0
  20. package/dist/src/dockerCleanup.d.ts +38 -0
  21. package/dist/src/dockerCleanup.js +77 -0
  22. package/dist/src/dockerCompose.d.ts +154 -0
  23. package/dist/src/dockerCompose.js +439 -0
  24. package/dist/src/fs.d.ts +5 -0
  25. package/dist/src/fs.js +38 -0
  26. package/dist/src/git.d.ts +9 -0
  27. package/dist/src/git.js +24 -0
  28. package/dist/src/index.d.ts +20 -0
  29. package/dist/src/index.js +20 -0
  30. package/dist/src/maps.d.ts +33 -0
  31. package/dist/src/maps.js +41 -0
  32. package/dist/src/npm.d.ts +35 -0
  33. package/dist/src/npm.js +104 -0
  34. package/dist/src/pkg.d.ts +33 -0
  35. package/dist/src/pkg.js +33 -0
  36. package/dist/src/publish.d.ts +27 -0
  37. package/dist/src/publish.js +57 -0
  38. package/dist/src/run.d.ts +13 -0
  39. package/dist/src/run.js +44 -0
  40. package/dist/src/tgz.d.ts +13 -0
  41. package/dist/src/tgz.js +67 -0
  42. package/dist/src/version.d.ts +34 -0
  43. package/dist/src/version.js +101 -0
  44. package/dist/src/yarn.d.ts +26 -0
  45. package/dist/src/yarn.js +75 -0
  46. package/package.json +74 -20
  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/ant.js +0 -42
  95. package/src/artifactory.js +0 -616
  96. package/src/cert.conf +0 -20
  97. package/src/cert.js +0 -152
  98. package/src/clean.js +0 -19
  99. package/src/copy.js +0 -79
  100. package/src/cp.js +0 -172
  101. package/src/docker.container.js +0 -389
  102. package/src/docker.js +0 -364
  103. package/src/docker.network.js +0 -159
  104. package/src/docker.volume.js +0 -136
  105. package/src/dockerCleanup.js +0 -151
  106. package/src/dockerCompose.js +0 -765
  107. package/src/flow.js +0 -20
  108. package/src/fs.js +0 -96
  109. package/src/git.js +0 -34
  110. package/src/hg.js +0 -34
  111. package/src/index.js +0 -26
  112. package/src/inline-resources.js +0 -157
  113. package/src/maps.js +0 -111
  114. package/src/npm.js +0 -144
  115. package/src/pkg.js +0 -80
  116. package/src/publish.js +0 -144
  117. package/src/run.js +0 -89
  118. package/src/test.js +0 -55
  119. package/src/tgz.js +0 -101
  120. package/src/version.js +0 -142
  121. package/src/yarn.js +0 -123
  122. package/test.js +0 -56
  123. package/test.js.flow +0 -55
  124. package/tgz.js +0 -102
  125. package/tgz.js.flow +0 -101
  126. package/version.js +0 -143
  127. package/version.js.flow +0 -142
  128. package/yarn.js +0 -124
  129. package/yarn.js.flow +0 -123
@@ -0,0 +1,62 @@
1
+ import { throttle } from 'lodash-es';
2
+ import { spawn } from './cp.js';
3
+ // calling this function in rapid succession can lead to errors.
4
+ // prefer throttled version
5
+ export async function unthrottledDockerVolumeLs({ filter, } = {}) {
6
+ return (await spawn('docker', ['volume', 'ls', '--format', '{{json .}}'], {
7
+ captureOutput: true,
8
+ })).output
9
+ .split('\n')
10
+ .filter((line) => !!line.trim())
11
+ .map((line) => {
12
+ const { Driver: driver, Labels, Mountpoint: mountPoint, Name: name, Scope: scope, } = JSON.parse(line.trim());
13
+ return {
14
+ driver,
15
+ inspect: async () => (await dockerVolumeInspect(name))?.[0] || null,
16
+ labels: Labels.split(',').map((l) => l.trim()),
17
+ mountPoint,
18
+ name,
19
+ rm: async () => dockerVolumeRm(name),
20
+ scope,
21
+ };
22
+ })
23
+ .filter((v) => v.name && (typeof filter === 'function' ? filter(v) : true));
24
+ }
25
+ export const dockerVolumeLs = throttle(unthrottledDockerVolumeLs, 500, {
26
+ trailing: false,
27
+ });
28
+ export async function dockerVolumeFind(search, options) {
29
+ return ((await dockerVolumeLs(options))?.find((v) => v.name === search || v.name.startsWith(search)) ?? null);
30
+ }
31
+ export async function dockerVolumeCreate(name, { labels = [], driver, driverOpts = {} } = {}) {
32
+ const existing = await dockerVolumeFind(name);
33
+ if (existing)
34
+ return existing;
35
+ await spawn('docker', [
36
+ 'volume',
37
+ 'create',
38
+ ...labels.flatMap((l) => ['--label', l]),
39
+ ...(driver ? ['-d', driver] : []),
40
+ ...Object.entries(driverOpts).flatMap(([k, v]) => ['-o', `${k}=${v}`]),
41
+ name,
42
+ ]);
43
+ return dockerVolumeFind(name);
44
+ }
45
+ export async function dockerVolumeRm(id, ignoreErrors = true) {
46
+ const ids = Array.isArray(id) ? id : [id];
47
+ try {
48
+ await spawn('docker', ['volume', 'rm', ...ids]);
49
+ }
50
+ catch (e) {
51
+ if (!ignoreErrors) {
52
+ throw new Error(`Failed to remove volume(s): ${e.message}`);
53
+ }
54
+ }
55
+ }
56
+ // calling this function in rapid succession can lead to errors.
57
+ // prefer throttled version
58
+ export async function unthrottledDockerVolumeInspect(id) {
59
+ const ids = Array.isArray(id) ? id : [id];
60
+ return (JSON.parse((await spawn('docker', ['volume', 'inspect', '--format', '{{json .}}', ...ids], { captureOutput: true })).output.trim()) || []);
61
+ }
62
+ export const dockerVolumeInspect = throttle(unthrottledDockerVolumeInspect, 500, { trailing: true });
@@ -0,0 +1,38 @@
1
+ interface CommonDockerCleanupOptions {
2
+ /** print matched items to console instead of deleting */
3
+ dryRun?: boolean;
4
+ }
5
+ /**
6
+ * Purge stopped containers
7
+ */
8
+ export declare const dockerContainerCleanup: ({ dryRun, }?: CommonDockerCleanupOptions) => Promise<void>;
9
+ interface DockerImageCleanupOptions extends CommonDockerCleanupOptions {
10
+ /** Image ids known to be produced by this build */
11
+ buildImageIds?: string[];
12
+ /** Explicit image names/ids to delete unconditionally */
13
+ images?: string[];
14
+ /**
15
+ * ignore default exclusions of :latest* and images < 1hr old
16
+ * this will delete all images in the given repos
17
+ */
18
+ purgeAll?: boolean;
19
+ /**
20
+ * looks for other builds of the same repos
21
+ * this will still exclude :latest* and images < 1hr old
22
+ * useful to clean up after past builds that failed to clean themselves properly
23
+ */
24
+ purgeOld?: boolean;
25
+ /** Image repositories to clean (defaults to the one configured in package.json) */
26
+ repos?: string[];
27
+ }
28
+ /**
29
+ * Purge docker images created during build process
30
+ */
31
+ export declare const dockerImageCleanup: ({ repos, buildImageIds, images, purgeAll, purgeOld, dryRun, }?: DockerImageCleanupOptions) => Promise<void>;
32
+ interface DockerCleanupOptions extends DockerImageCleanupOptions, CommonDockerCleanupOptions {
33
+ }
34
+ /**
35
+ * Purge docker images created during build process
36
+ */
37
+ export declare const dockerCleanup: ({ dryRun, ...rest }?: DockerCleanupOptions) => Promise<void>;
38
+ export default dockerCleanup;
@@ -0,0 +1,77 @@
1
+ import { uniq } from 'lodash-es';
2
+ import moment from 'moment';
3
+ import { dockerContainerLs } from './docker.container.js';
4
+ import { dockerImages, dockerRmi, getDockerRepo, getDockerTags, getUntaggedDockerIds, } from './docker.js';
5
+ import { buildLog } from './run.js';
6
+ // flattens arrays of promises and/or promises for arrays
7
+ // into a single array of (awaited) values
8
+ async function flatten(input) {
9
+ const result = await input;
10
+ return Array.isArray(result)
11
+ ? [].concat(...(await Promise.all(result.map(flatten))))
12
+ : result;
13
+ }
14
+ /**
15
+ * Purge stopped containers
16
+ */
17
+ export const dockerContainerCleanup = async ({ dryRun = process.argv.includes('--dry-run'), } = {}) => {
18
+ const toClean = await dockerContainerLs({
19
+ all: true,
20
+ filter: (c) => c.exited,
21
+ });
22
+ if (dryRun) {
23
+ if (toClean) {
24
+ buildLog(`Containers to delete (dry run):\n ${toClean
25
+ .map((c) => c.id)
26
+ .join('\n ')}`);
27
+ }
28
+ else {
29
+ buildLog('No containers to delete (dry run)');
30
+ }
31
+ }
32
+ else if (toClean) {
33
+ await Promise.all(toClean.map(async (c) => c.rm(true)));
34
+ }
35
+ };
36
+ /**
37
+ * Purge docker images created during build process
38
+ */
39
+ export const dockerImageCleanup = async ({ repos = [getDockerRepo()], buildImageIds = [], images = [], purgeAll = process.argv.includes('--purge-all'), purgeOld = !process.argv.includes('--keep-old'), dryRun = process.argv.includes('--dry-run'), } = {}) => {
40
+ // only match images without a :latest tag
41
+ const filterLatest = (m) => purgeAll || !m.tag.match(/^latest(-\w+)?$/);
42
+ // only match images more than an hour old, AND without a :latest* tag
43
+ const filterOld = (m) => purgeAll ||
44
+ (filterLatest(m) && moment().subtract(1, 'h').isAfter(m.created));
45
+ const uniqueList = uniq([
46
+ // the things that we know were just built by us
47
+ // remove all tags from the given repos with a matching `buildImageIds`, except for :latest*
48
+ ...(await flatten(repos.map((r) => buildImageIds.map(async (b) => getDockerTags(b, r, true, filterLatest))))),
49
+ // unconditionally delete explicitly passed images/ids
50
+ ...images,
51
+ // images in the given `repos` without a tag, older than 1hr
52
+ ...(await flatten(repos.map(async (r) => getUntaggedDockerIds(r, filterOld)))),
53
+ // images with no repo and no tag
54
+ ...(await dockerImages(null, (m) => m.repository === '<none>' && m.tag === '<none>' && filterOld(m))).map((m) => m.id),
55
+ // any leftover tags from prior builds (only if purgeAll or purgeOld)
56
+ ...(purgeAll || purgeOld
57
+ ? await flatten(repos.map(async (r) => getDockerTags(null, r, true, filterOld)))
58
+ : []),
59
+ ]);
60
+ if (dryRun) {
61
+ buildLog(`Images to delete (dry run):\n ${uniqueList.join('\n ')}`);
62
+ }
63
+ else {
64
+ await dockerRmi(uniqueList, true);
65
+ }
66
+ };
67
+ /**
68
+ * Purge docker images created during build process
69
+ */
70
+ export const dockerCleanup = async ({ dryRun, ...rest } = {}) => {
71
+ await dockerContainerCleanup({ dryRun });
72
+ await dockerImageCleanup({
73
+ dryRun,
74
+ ...rest,
75
+ });
76
+ };
77
+ export default dockerCleanup;
@@ -0,0 +1,154 @@
1
+ import { DockerContainer } from './docker.container.js';
2
+ import { DockerNetwork, DockerNetworkCreateOptions } from './docker.network.js';
3
+ import { DockerVolume, DockerVolumeCreateOptions } from './docker.volume.js';
4
+ export declare type DockerComposeResourceTracker = {
5
+ cleaning: null | Promise<void>;
6
+ containers: Map<string, DockerContainer>;
7
+ networks: Map<string, DockerNetwork>;
8
+ services: Map<string, DockerComposeService>;
9
+ volumes: Map<string, DockerVolume>;
10
+ };
11
+ export declare const dockerComposeCreateResourceTracker: () => DockerComposeResourceTracker;
12
+ declare type DockerComposeEnvDef = string[] | Record<string, string>;
13
+ declare type DockerComposeULimitDef = number | {
14
+ hard: number;
15
+ soft: number;
16
+ };
17
+ declare type DockerComposeULimitsDef = Record<string, DockerComposeULimitDef>;
18
+ interface DockerComposeServiceDef {
19
+ command?: string[];
20
+ container_name?: string;
21
+ environment?: DockerComposeEnvDef;
22
+ image?: string;
23
+ links?: string[];
24
+ networks?: string[];
25
+ ports?: string[];
26
+ ulimits?: DockerComposeULimitsDef;
27
+ volumes?: string[];
28
+ }
29
+ interface DockerComposeVolumeDef extends DockerVolumeCreateOptions {
30
+ name?: string;
31
+ }
32
+ interface DockerComposeNetworkDef extends DockerNetworkCreateOptions {
33
+ name?: string;
34
+ }
35
+ interface DockerCompose {
36
+ networks?: Record<string, DockerComposeNetworkDef>;
37
+ services?: Record<string, DockerComposeServiceDef>;
38
+ version: string;
39
+ volumes?: Record<string, DockerComposeVolumeDef>;
40
+ }
41
+ /**
42
+ * Parse a docker-compose.yml file into a pojo representation
43
+ * @param {string} filePath Path to the docker-compose.yml file
44
+ */
45
+ export declare const dockerComposeParse: (filePath?: string) => Promise<DockerCompose>;
46
+ /**
47
+ * Get the main docker-compose.yml from the root of the repo
48
+ * @param {boolean} force Force reload from file
49
+ */
50
+ export declare const dockerComposeGetMain: (force?: boolean) => Promise<DockerCompose>;
51
+ interface PortMap {
52
+ docker: number;
53
+ local: null | number;
54
+ localDefault: number;
55
+ }
56
+ interface UrlMap {
57
+ docker: null | string;
58
+ local: null | string;
59
+ }
60
+ interface ParsedPortInfo {
61
+ ports: PortMap[];
62
+ runArgs: string[];
63
+ }
64
+ interface ParsedVolumeInfo {
65
+ runArgs: string[];
66
+ volumes: string[];
67
+ }
68
+ interface ParsedNetworkInfo {
69
+ networks: string[];
70
+ runArgs: string[];
71
+ }
72
+ export declare const dockerComposeCreateNetworks: (names?: string[] | null | undefined, dockerCompose?: string | DockerCompose | null | undefined, tracker?: DockerComposeResourceTracker) => Promise<void>;
73
+ export declare const dockerComposeCreateVolumes: (names?: string[] | undefined, dockerCompose?: string | DockerCompose | null | undefined, tracker?: DockerComposeResourceTracker) => Promise<void>;
74
+ declare type HealthCheckFn = (result: UpResult) => Promise<void>;
75
+ interface UpOptions {
76
+ avoidConflicts?: boolean;
77
+ cmd?: string[];
78
+ healthCheck?: HealthCheckFn;
79
+ mapPorts?: boolean;
80
+ mapVolumes?: boolean;
81
+ tracker?: DockerComposeResourceTracker;
82
+ }
83
+ interface ServiceNameInfo {
84
+ aliases: string[];
85
+ name: string;
86
+ suffix: string;
87
+ }
88
+ interface UpResult {
89
+ aliases: string[];
90
+ container: DockerContainer;
91
+ containerName: string;
92
+ ports: PortMap[];
93
+ service: DockerComposeService;
94
+ urls: UrlMap[];
95
+ volumes: string[];
96
+ }
97
+ export declare class DockerComposeService {
98
+ #private;
99
+ constructor(name: string, serviceDef: DockerComposeServiceDef, dockerCompose?: null | (DockerCompose | string));
100
+ get name(): string;
101
+ get serviceDef(): DockerComposeServiceDef;
102
+ get image(): null | string;
103
+ get defaultContainerName(): string;
104
+ get cmd(): string[];
105
+ get envArgs(): string[];
106
+ /**
107
+ * Find (available) container name and alias(es)
108
+ * @param {boolean} avoidConflicts if `true`, find a container name that is unused
109
+ * @param {number} max maximum number of names to try
110
+ */
111
+ getNames(avoidConflicts?: boolean, max?: number): Promise<ServiceNameInfo>;
112
+ getPorts(avoidConflicts?: boolean, mapLocal?: boolean): Promise<ParsedPortInfo>;
113
+ getDefaultPortInfo(): Promise<ParsedPortInfo>;
114
+ getDefaultPorts(): Promise<PortMap[]>;
115
+ getDefaultPortArgs(): Promise<string[]>;
116
+ getDefaultUrls(): Promise<UrlMap[]>;
117
+ getDefaultNetworkInfo(): Promise<ParsedNetworkInfo>;
118
+ getDefaultNetworks(): Promise<string[]>;
119
+ getDefaultNetworkArgs(): Promise<string[]>;
120
+ getDefaultVolumeInfo(): Promise<ParsedVolumeInfo>;
121
+ getDefaultVolumes(): Promise<string[]>;
122
+ getDefaultVolumeArgs(): Promise<string[]>;
123
+ pull(): Promise<void>;
124
+ getBaseRunArgs(): Promise<string[]>;
125
+ assembleRunArgs(containerName?: string, aliases?: string[], additionalArgs?: null | string[]): Promise<string[]>;
126
+ getRunArgs(avoidConflicts?: boolean, mapLocalPorts?: boolean): Promise<string[]>;
127
+ up({ healthCheck, avoidConflicts, mapVolumes, mapPorts, cmd, tracker, }?: UpOptions): Promise<UpResult>;
128
+ down({ includeDefaults, includeNetworks, includeVolumes, }?: {
129
+ includeDefaults?: boolean;
130
+ includeNetworks?: boolean;
131
+ includeVolumes?: boolean;
132
+ }): Promise<void>;
133
+ static parse(name: string, force?: boolean, dockerCompose?: null | (DockerCompose | string)): Promise<null | DockerComposeService>;
134
+ static getUrls(ports: PortMap[], alias: string): UrlMap[];
135
+ }
136
+ export declare function dockerComposeGetAllServices(dockerCompose?: null | (string | DockerCompose)): Promise<Record<string, null | DockerComposeService>>;
137
+ export declare function dockerComposeTeardown({ includeDefaults, includeVolumes, includeNetworks, dockerCompose, tracker, }?: {
138
+ dockerCompose?: null | (string | DockerCompose);
139
+ includeDefaults?: boolean;
140
+ includeNetworks?: boolean;
141
+ includeVolumes?: boolean;
142
+ tracker?: DockerComposeResourceTracker;
143
+ }): Promise<void>;
144
+ export interface DockerContainerInfo extends UpResult {
145
+ dockerPort?: null | number;
146
+ dockerUrl?: null | string;
147
+ id: string;
148
+ image: string;
149
+ name?: null | string;
150
+ port?: null | number;
151
+ url?: null | string;
152
+ }
153
+ export declare function dockerComposeRunService(key: string, upOptions?: UpOptions, dockerCompose?: null | string | DockerCompose): Promise<DockerContainerInfo>;
154
+ export {};