@driveflux/vercel 1.0.0-next.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 (42) hide show
  1. package/.eslintrc +3 -0
  2. package/.turbo/turbo-build.log +6 -0
  3. package/CHANGELOG.md +7 -0
  4. package/deployment/build-vercel-url.d.ts.d.ts +1 -0
  5. package/deployment/build-vercel-url.js.cjs +1 -0
  6. package/deployment/make-deployment-lockfile.d.ts.d.ts +1 -0
  7. package/deployment/make-deployment-lockfile.js.cjs +1 -0
  8. package/deployment/use-deployment-lockfile.d.ts.d.ts +1 -0
  9. package/deployment/use-deployment-lockfile.js.cjs +1 -0
  10. package/deployment/utils.d.ts.d.ts +1 -0
  11. package/deployment/utils.js.cjs +1 -0
  12. package/dist/cjs/deployment/build-vercel-url.js +59 -0
  13. package/dist/cjs/deployment/make-deployment-lockfile.js +186 -0
  14. package/dist/cjs/deployment/use-deployment-lockfile.js +191 -0
  15. package/dist/cjs/deployment/utils.js +535 -0
  16. package/dist/cjs/index.js +12 -0
  17. package/dist/cjs/package.json +3 -0
  18. package/dist/deployment/build-vercel-url.d.ts +4 -0
  19. package/dist/deployment/build-vercel-url.d.ts.map +1 -0
  20. package/dist/deployment/build-vercel-url.js +33 -0
  21. package/dist/deployment/make-deployment-lockfile.d.ts +2 -0
  22. package/dist/deployment/make-deployment-lockfile.d.ts.map +1 -0
  23. package/dist/deployment/make-deployment-lockfile.js +182 -0
  24. package/dist/deployment/use-deployment-lockfile.d.ts +2 -0
  25. package/dist/deployment/use-deployment-lockfile.d.ts.map +1 -0
  26. package/dist/deployment/use-deployment-lockfile.js +187 -0
  27. package/dist/deployment/utils.d.ts +6 -0
  28. package/dist/deployment/utils.d.ts.map +1 -0
  29. package/dist/deployment/utils.js +503 -0
  30. package/dist/index.d.ts +3 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +2 -0
  33. package/index.cjs +1 -0
  34. package/index.d.ts +2 -0
  35. package/package.json +35 -0
  36. package/src/deployment/build-vercel-url.ts +47 -0
  37. package/src/deployment/make-deployment-lockfile.ts +22 -0
  38. package/src/deployment/use-deployment-lockfile.ts +20 -0
  39. package/src/deployment/utils.ts +151 -0
  40. package/src/index.ts +2 -0
  41. package/tsconfig.json +12 -0
  42. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,503 @@
1
+ // @ts-check
2
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
3
+ try {
4
+ var info = gen[key](arg);
5
+ var value = info.value;
6
+ } catch (error) {
7
+ reject(error);
8
+ return;
9
+ }
10
+ if (info.done) {
11
+ resolve(value);
12
+ } else {
13
+ Promise.resolve(value).then(_next, _throw);
14
+ }
15
+ }
16
+ function _async_to_generator(fn) {
17
+ return function() {
18
+ var self = this, args = arguments;
19
+ return new Promise(function(resolve, reject) {
20
+ var gen = fn.apply(self, args);
21
+ function _next(value) {
22
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
23
+ }
24
+ function _throw(err) {
25
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
26
+ }
27
+ _next(undefined);
28
+ });
29
+ };
30
+ }
31
+ function _ts_generator(thisArg, body) {
32
+ var f, y, t, g, _ = {
33
+ label: 0,
34
+ sent: function() {
35
+ if (t[0] & 1) throw t[1];
36
+ return t[1];
37
+ },
38
+ trys: [],
39
+ ops: []
40
+ };
41
+ return g = {
42
+ next: verb(0),
43
+ "throw": verb(1),
44
+ "return": verb(2)
45
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
46
+ return this;
47
+ }), g;
48
+ function verb(n) {
49
+ return function(v) {
50
+ return step([
51
+ n,
52
+ v
53
+ ]);
54
+ };
55
+ }
56
+ function step(op) {
57
+ if (f) throw new TypeError("Generator is already executing.");
58
+ while(_)try {
59
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
60
+ if (y = 0, t) op = [
61
+ op[0] & 2,
62
+ t.value
63
+ ];
64
+ switch(op[0]){
65
+ case 0:
66
+ case 1:
67
+ t = op;
68
+ break;
69
+ case 4:
70
+ _.label++;
71
+ return {
72
+ value: op[1],
73
+ done: false
74
+ };
75
+ case 5:
76
+ _.label++;
77
+ y = op[1];
78
+ op = [
79
+ 0
80
+ ];
81
+ continue;
82
+ case 7:
83
+ op = _.ops.pop();
84
+ _.trys.pop();
85
+ continue;
86
+ default:
87
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
88
+ _ = 0;
89
+ continue;
90
+ }
91
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
92
+ _.label = op[1];
93
+ break;
94
+ }
95
+ if (op[0] === 6 && _.label < t[1]) {
96
+ _.label = t[1];
97
+ t = op;
98
+ break;
99
+ }
100
+ if (t && _.label < t[2]) {
101
+ _.label = t[2];
102
+ _.ops.push(op);
103
+ break;
104
+ }
105
+ if (t[2]) _.ops.pop();
106
+ _.trys.pop();
107
+ continue;
108
+ }
109
+ op = body.call(thisArg, _);
110
+ } catch (e) {
111
+ op = [
112
+ 6,
113
+ e
114
+ ];
115
+ y = 0;
116
+ } finally{
117
+ f = t = 0;
118
+ }
119
+ if (op[0] & 5) throw op[1];
120
+ return {
121
+ value: op[0] ? op[1] : void 0,
122
+ done: true
123
+ };
124
+ }
125
+ }
126
+ import { spawn } from "node:child_process";
127
+ import crypto from "node:crypto";
128
+ import fs from "node:fs/promises";
129
+ import path from "node:path";
130
+ var basePath = process.cwd();
131
+ export var copyDeploymentLockfile = function() {
132
+ var _ref = _async_to_generator(function() {
133
+ var stat;
134
+ return _ts_generator(this, function(_state) {
135
+ switch(_state.label){
136
+ case 0:
137
+ return [
138
+ 4,
139
+ fs.stat("".concat(basePath, "/pnpm-lock-production.yaml"))
140
+ ];
141
+ case 1:
142
+ stat = _state.sent();
143
+ if (!stat.isFile()) return [
144
+ 3,
145
+ 3
146
+ ];
147
+ // If there is, copy it to pnpm-lock.yaml
148
+ return [
149
+ 4,
150
+ fs.copyFile("".concat(basePath, "/pnpm-lock-production.yaml"), "".concat(basePath, "/pnpm-lock.yaml"))
151
+ ];
152
+ case 2:
153
+ _state.sent();
154
+ _state.label = 3;
155
+ case 3:
156
+ return [
157
+ 2
158
+ ];
159
+ }
160
+ });
161
+ });
162
+ return function copyDeploymentLockfile() {
163
+ return _ref.apply(this, arguments);
164
+ };
165
+ }();
166
+ export var removePackagesFromWorkspace = function() {
167
+ var _ref = _async_to_generator(function() {
168
+ var workspacePath, workspaceContent, newContent;
169
+ return _ts_generator(this, function(_state) {
170
+ switch(_state.label){
171
+ case 0:
172
+ workspacePath = path.resolve(basePath, "./pnpm-workspace.yaml");
173
+ // Make a copy of the workspace
174
+ return [
175
+ 4,
176
+ fs.copyFile(workspacePath, "".concat(basePath, "/pnpm-workspace.yaml.bak"))
177
+ ];
178
+ case 1:
179
+ _state.sent();
180
+ return [
181
+ 4,
182
+ fs.readFile(workspacePath, "utf8")
183
+ ];
184
+ case 2:
185
+ workspaceContent = _state.sent();
186
+ newContent = workspaceContent.replace(" - 'packages/*'\n", "");
187
+ // Write the workspace without packages/*
188
+ return [
189
+ 4,
190
+ fs.writeFile(workspacePath, newContent)
191
+ ];
192
+ case 3:
193
+ _state.sent();
194
+ return [
195
+ 2
196
+ ];
197
+ }
198
+ });
199
+ });
200
+ return function removePackagesFromWorkspace() {
201
+ return _ref.apply(this, arguments);
202
+ };
203
+ }();
204
+ export var makeDeploymentLockfile = function() {
205
+ var _ref = _async_to_generator(function() {
206
+ var lockfileChanged, error, output, e;
207
+ return _ts_generator(this, function(_state) {
208
+ switch(_state.label){
209
+ case 0:
210
+ return [
211
+ 4,
212
+ hasLockfileChanged()
213
+ ];
214
+ case 1:
215
+ lockfileChanged = _state.sent();
216
+ if (!lockfileChanged) {
217
+ return [
218
+ 2
219
+ ];
220
+ }
221
+ console.log("Lockfile changed, generating production lockfile...");
222
+ return [
223
+ 4,
224
+ removePackagesFromWorkspace()
225
+ ];
226
+ case 2:
227
+ _state.sent();
228
+ // Backup the lockfile
229
+ return [
230
+ 4,
231
+ fs.rename("".concat(basePath, "/pnpm-lock.yaml"), "".concat(basePath, "/pnpm-lock.yaml.bak"))
232
+ ];
233
+ case 3:
234
+ _state.sent();
235
+ _state.label = 4;
236
+ case 4:
237
+ _state.trys.push([
238
+ 4,
239
+ 6,
240
+ ,
241
+ 7
242
+ ]);
243
+ return [
244
+ 4,
245
+ copyDeploymentLockfile()
246
+ ];
247
+ case 5:
248
+ _state.sent();
249
+ return [
250
+ 3,
251
+ 7
252
+ ];
253
+ case 6:
254
+ error = _state.sent();
255
+ return [
256
+ 3,
257
+ 7
258
+ ];
259
+ case 7:
260
+ _state.trys.push([
261
+ 7,
262
+ 12,
263
+ ,
264
+ 13
265
+ ]);
266
+ // Now we run pnpm i --lockfile-only to generate the lockfile without installing
267
+ return [
268
+ 4,
269
+ runCommand("pnpm i --lockfile-only")
270
+ ];
271
+ case 8:
272
+ _state.sent();
273
+ // Let's copy the lockfile to pnpm-lock-production.yaml
274
+ output = "".concat(basePath, "/pnpm-lock-production.yaml");
275
+ return [
276
+ 4,
277
+ fs.rename("".concat(basePath, "/pnpm-lock.yaml"), output)
278
+ ];
279
+ case 9:
280
+ _state.sent();
281
+ if (!lockfileChanged) return [
282
+ 3,
283
+ 11
284
+ ];
285
+ // Now write the hash
286
+ return [
287
+ 4,
288
+ fs.writeFile("".concat(basePath, "/pnpm-lock.yaml.hash"), lockfileChanged)
289
+ ];
290
+ case 10:
291
+ _state.sent();
292
+ _state.label = 11;
293
+ case 11:
294
+ console.log("Generated production lockfile at ".concat(output));
295
+ return [
296
+ 3,
297
+ 13
298
+ ];
299
+ case 12:
300
+ e = _state.sent();
301
+ if (e.output) {
302
+ console.log(e.output);
303
+ console.log("\uD83D\uDEA8 You might have upgraded packages but did not publish them yet. If it's the case, then:");
304
+ console.log("1. Run build:workspace and make sure there are no errors");
305
+ console.log('2. Run "PNPM_PUBLISH_FLAGS="--no-git-checks" pnpm run publish:all". You must have the NPM_TOKEN with publish rights');
306
+ console.log("3. Run make-deployment-lockfile again (or commit and let the hook run it for you)");
307
+ } else {
308
+ console.error(e);
309
+ }
310
+ return [
311
+ 3,
312
+ 13
313
+ ];
314
+ case 13:
315
+ //
316
+ return [
317
+ 4,
318
+ restoreLockfile()
319
+ ];
320
+ case 14:
321
+ _state.sent();
322
+ return [
323
+ 2
324
+ ];
325
+ }
326
+ });
327
+ });
328
+ return function makeDeploymentLockfile() {
329
+ return _ref.apply(this, arguments);
330
+ };
331
+ }();
332
+ /**
333
+ *
334
+ * @returns {Promise<string | false>}
335
+ */ var hasLockfileChanged = function() {
336
+ var _ref = _async_to_generator(function() {
337
+ var newHash, _, _1, previousHash, error;
338
+ return _ts_generator(this, function(_state) {
339
+ switch(_state.label){
340
+ case 0:
341
+ _1 = (_ = crypto.createHash("sha256")).update;
342
+ return [
343
+ 4,
344
+ fs.readFile("".concat(basePath, "/pnpm-lock.yaml"), "utf8")
345
+ ];
346
+ case 1:
347
+ newHash = _1.apply(_, [
348
+ _state.sent()
349
+ ]).digest("hex");
350
+ _state.label = 2;
351
+ case 2:
352
+ _state.trys.push([
353
+ 2,
354
+ 4,
355
+ ,
356
+ 5
357
+ ]);
358
+ return [
359
+ 4,
360
+ fs.readFile("".concat(basePath, "/pnpm-lock.yaml.hash"), "utf8")
361
+ ];
362
+ case 3:
363
+ previousHash = _state.sent();
364
+ return [
365
+ 2,
366
+ previousHash !== newHash ? newHash : false
367
+ ];
368
+ case 4:
369
+ error = _state.sent();
370
+ // If the command fails, assume the lockfile has changed to be on the safe side
371
+ return [
372
+ 2,
373
+ newHash
374
+ ];
375
+ case 5:
376
+ return [
377
+ 2
378
+ ];
379
+ }
380
+ });
381
+ });
382
+ return function hasLockfileChanged() {
383
+ return _ref.apply(this, arguments);
384
+ };
385
+ }();
386
+ export var runCommand = function(command) {
387
+ return new Promise(function(resolve, reject) {
388
+ var output = "";
389
+ var finalCommand = command.split(" ");
390
+ var commandName = finalCommand.shift();
391
+ if (!commandName) {
392
+ return reject("No command to run");
393
+ }
394
+ var c = spawn(commandName, finalCommand);
395
+ c.stdout.on("data", function(data) {
396
+ output += data.toString();
397
+ });
398
+ c.stderr.on("error", function(error) {
399
+ output += error.toString();
400
+ });
401
+ c.on("error", function(error) {
402
+ reject(error.toString());
403
+ });
404
+ c.on("exit", function(code) {
405
+ if (code) {
406
+ var error = new Error("Command ".concat(command, " exited with code ").concat(code));
407
+ // @ts-expect-error
408
+ error.output = output;
409
+ reject(error);
410
+ } else {
411
+ resolve(0);
412
+ }
413
+ });
414
+ });
415
+ };
416
+ export var restoreLockfile = function() {
417
+ var _ref = _async_to_generator(function() {
418
+ var lockStat, e, workspaceStat, e1;
419
+ return _ts_generator(this, function(_state) {
420
+ switch(_state.label){
421
+ case 0:
422
+ _state.trys.push([
423
+ 0,
424
+ 4,
425
+ ,
426
+ 5
427
+ ]);
428
+ return [
429
+ 4,
430
+ fs.stat("".concat(basePath, "/pnpm-lock.yaml.bak"))
431
+ ];
432
+ case 1:
433
+ lockStat = _state.sent();
434
+ if (!lockStat.isFile()) return [
435
+ 3,
436
+ 3
437
+ ];
438
+ // Now we restore lockfile
439
+ return [
440
+ 4,
441
+ fs.rename("".concat(basePath, "/pnpm-lock.yaml.bak"), "".concat(basePath, "/pnpm-lock.yaml"))
442
+ ];
443
+ case 2:
444
+ _state.sent();
445
+ _state.label = 3;
446
+ case 3:
447
+ return [
448
+ 3,
449
+ 5
450
+ ];
451
+ case 4:
452
+ e = _state.sent();
453
+ return [
454
+ 3,
455
+ 5
456
+ ];
457
+ case 5:
458
+ _state.trys.push([
459
+ 5,
460
+ 9,
461
+ ,
462
+ 10
463
+ ]);
464
+ return [
465
+ 4,
466
+ fs.stat("".concat(basePath, "/pnpm-workspace.yaml.bak"))
467
+ ];
468
+ case 6:
469
+ workspaceStat = _state.sent();
470
+ if (!workspaceStat.isFile()) return [
471
+ 3,
472
+ 8
473
+ ];
474
+ // Restore the workspace
475
+ return [
476
+ 4,
477
+ fs.rename("".concat(basePath, "/pnpm-workspace.yaml.bak"), "".concat(basePath, "/pnpm-workspace.yaml"))
478
+ ];
479
+ case 7:
480
+ _state.sent();
481
+ _state.label = 8;
482
+ case 8:
483
+ return [
484
+ 3,
485
+ 10
486
+ ];
487
+ case 9:
488
+ e1 = _state.sent();
489
+ return [
490
+ 3,
491
+ 10
492
+ ];
493
+ case 10:
494
+ return [
495
+ 2
496
+ ];
497
+ }
498
+ });
499
+ });
500
+ return function restoreLockfile() {
501
+ return _ref.apply(this, arguments);
502
+ };
503
+ }();
@@ -0,0 +1,3 @@
1
+ declare const _default: {};
2
+ export default _default;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,wBAAkB"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ // Todo
2
+ export default {};
package/index.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/cjs/index.js')
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './dist/index'
2
+ export { default } from './dist/index.d.ts'
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@driveflux/vercel",
3
+ "version": "1.0.0-next.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "import": "./dist/index.js",
8
+ "require": "./dist/cjs/index.js",
9
+ "types": "./dist/index.d.ts"
10
+ },
11
+ "./deployment/*": {
12
+ "import": "./dist/deployment/*.js",
13
+ "require": "./dist/cjs/deployment/*.js",
14
+ "types": "./dist/deployment/*.d.ts"
15
+ }
16
+ },
17
+ "devDependencies": {
18
+ "@driveflux/tsconfig": "1.0.0-next.0",
19
+ "@driveflux/eslint-config": "1.0.0-next.0",
20
+ "@driveflux/fab": "1.0.0-next.0",
21
+ "@swc/cli": "^0.1.63",
22
+ "@swc/core": "^1.3.99",
23
+ "@types/lodash": "^4.14.202",
24
+ "@types/node": "^20.9.5",
25
+ "del-cli": "^5.1.0",
26
+ "eslint": "8.54.0",
27
+ "typescript": "^5.3.3"
28
+ },
29
+ "scripts": {
30
+ "build": "fab",
31
+ "clean": "fab clean",
32
+ "types": "tsc --noEmit",
33
+ "lint": "eslint \"src/**/*.ts\""
34
+ }
35
+ }
@@ -0,0 +1,47 @@
1
+ import crypto from 'crypto'
2
+
3
+ const slugify = (str: string) => {
4
+ const slug = str
5
+ .toLowerCase() // Convert to lowercase
6
+ .replace(/\//, '-') // replace one forward slash with hyphen
7
+ .replace(/[^a-z0-9-]/g, '') // Replace any non-alphanumeric characters with hyphens
8
+ .replace(/-+/g, '-') // Replace any consecutive hyphens with a single hyphen
9
+ .replace(/^-|-$/g, '') // Remove any leading or trailing hyphens
10
+
11
+ return slug;
12
+ };
13
+
14
+ export const truncate = (
15
+ prefix: string,
16
+ branch: string,
17
+ projectName: string,
18
+ org: string
19
+ ) => {
20
+ const branchSlug = slugify(branch);
21
+ const deploymentName = `${projectName}-${prefix}-${branchSlug}-${org}`;
22
+ if (deploymentName.length < 64) {
23
+ return `https://${deploymentName}.vercel.app`;
24
+ }
25
+
26
+ const hash = crypto
27
+ .createHash('sha256')
28
+ .update(`git-${branch}${projectName}`)
29
+ .digest('hex')
30
+ .slice(0, 6);
31
+
32
+ const suffix = `${hash.slice(0, 6)}-${org}`;
33
+ const truncatedName = deploymentName.slice(0, 61 - suffix.length);
34
+ return `https://${truncatedName}-${suffix}.vercel.app`;
35
+ };
36
+
37
+ export const buildVercelUrl = (projectName: string, branchName: string): string => {
38
+ const scopeSlug = 'driveflux'
39
+ const prefix = 'git'
40
+ return truncate(prefix, branchName, projectName, scopeSlug)
41
+ }
42
+
43
+
44
+ export const isPreviewButNotStaging = (): boolean => {
45
+ const KNOWN_BRANCHES = ['develop', 'master']
46
+ return !!process.env.VERCEL_GIT_COMMIT_REF && !KNOWN_BRANCHES.includes(process.env.VERCEL_GIT_COMMIT_REF)
47
+ }
@@ -0,0 +1,22 @@
1
+ // @ts-check
2
+ import { exec as oldExec } from 'node:child_process'
3
+ import { promisify } from 'node:util'
4
+ import { makeDeploymentLockfile, restoreLockfile } from './utils.js'
5
+
6
+ const exec = promisify(oldExec)
7
+
8
+ const start = async () => {
9
+ try {
10
+ await makeDeploymentLockfile()
11
+ await exec('git add -A')
12
+ } finally {
13
+ await restoreLockfile()
14
+ }
15
+ process.exit(0)
16
+ }
17
+
18
+ process.on('SIGINT', restoreLockfile)
19
+
20
+ start().catch((error) => {
21
+ console.error('An error occurred:', error)
22
+ })
@@ -0,0 +1,20 @@
1
+ // @ts-check
2
+ import { copyDeploymentLockfile, makeDeploymentLockfile, removePackagesFromWorkspace } from './utils.js'
3
+
4
+ const useDeploymentLockfile = async () => {
5
+ try {
6
+ await copyDeploymentLockfile()
7
+ } catch(e) {
8
+ // Something went wrong, let's try to make the production lockfile
9
+ await makeDeploymentLockfile()
10
+
11
+ // Now we copy the file again
12
+ await copyDeploymentLockfile()
13
+ } finally {
14
+ removePackagesFromWorkspace()
15
+ }
16
+ }
17
+
18
+ useDeploymentLockfile().catch((error) => {
19
+ console.error('An error occurred:', error)
20
+ })