@samhammer/migrate-mongo-sag 1.0.1 → 1.1.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.
package/README.md CHANGED
@@ -6,7 +6,7 @@ it provides some additional functionality to the original migrate-mongo https://
6
6
  this tool additionally supports:
7
7
 
8
8
  - execution of migration-scripts for "brand" and "default" (will be merged)
9
- - new options to set "brand", "env" and "suffix" as environment variables
9
+ - new options to set "brand", "env" and "app" as environment variables
10
10
  - new command "dropDatabase" to delete the configured database
11
11
  - load settings from .env files by https://github.com/motdotla/dotenv
12
12
  - load vault secrets defined in .env files https://github.com/SamhammerAG/vault-client-sag
@@ -32,7 +32,7 @@ Options:
32
32
  -V, --version output the version number
33
33
  -e, --env <env> set process.env.Environment
34
34
  -b, --brand <brand> set process.env.Brand
35
- -s, --suffix <suffix> set process.env.Suffix
35
+ -a, --app <app> set process.env.App
36
36
  -t, --trace set process.env.TRACE to enable trace outputs
37
37
  -h, --help display help for command
38
38
 
@@ -1,2 +1,2 @@
1
1
  #! /usr/bin/env node
2
- "use strict";const s=require("migrate-mongo"),h=require("commander"),p=require("dotenv"),u=require("dotenv-expand"),y=require("@samhammer/vault-client-sag"),d=require("lodash"),b=require("child_process"),i=require("path"),l=require("fs/promises"),m=require("fs"),D="@samhammer/migrate-mongo-sag",R="1.0.1",T={"migrate-mongo":"dist/migrate-mongo.js"},C=["dist/**/*"],A=["mongodb","migration"],$="SamhammerAG",x="MIT",k={build:"vite build","migrate-mongo":"vite build && node dist/migrate-mongo.js",format:"prettier --write --parser typescript ./src",lint:"eslint ./src",test:"vitest run"},q={"@rushstack/eslint-patch":"^1.10.3","@types/lodash":"^4.17.5","@types/migrate-mongo":"^8.2.0","@types/node":"^18.19.34","@typescript-eslint/eslint-plugin":"^7.13.0","@typescript-eslint/parser":"^7.13.0",eslint:"^8.57.0","eslint-config-prettier":"^8.10.0","eslint-plugin-import":"^2.29.1","eslint-plugin-prettier":"^5.1.3",mongodb:"^4.17.2",prettier:"^3.3.2",typescript:"^5.4.5",vite:"^5.2.13",vitest:"^1.6.0"},M={"@samhammer/vault-client-sag":"^1.1.0",commander:"^12.1.0",dotenv:"^16.4.5","dotenv-expand":"^11.0.6",lodash:"^4.17.21","migrate-mongo":"^11.0.0"},B={mongodb:"^4.4.1 || ^5.0.0 || ^6.0.0"},S={node:"18.19.0",yarn:"4.0.2"},N={name:D,version:R,bin:T,files:C,keywords:A,author:$,license:x,scripts:k,devDependencies:q,dependencies:M,peerDependencies:B,volta:S},v=[".env.local",".env"];async function O(e){try{process.env.TRACE&&console.log("int env..."),await P(e),await I(),await F(),await j(),process.env.TRACE&&console.log("finished init env")}catch(n){console.error("init env failed",n)}}function j(){process.env.TRACE&&console.log("loading env files...");const e=p.config({path:v});u.expand({parsed:e.parsed})}async function F(){process.env.TRACE&&console.log("loading vault...");const e=V(v);process.env.TRACE&&console.log("requesting vault keys",e),await(await y.getVault()).loadSecretsToEnv(e),process.env.TRACE&&console.log("finished loading from vault")}function V(e){const n=d.clone(process.env),o=p.config({path:e,processEnv:n}),t=d.pickBy(o.parsed,c=>c.startsWith("VaultKey")),a=u.expand({parsed:t,processEnv:n});return d.invert(a.parsed)}async function P(e){process.env.TRACE&&console.log("loading command options...");const n=e.getOptionValue("env"),o=e.getOptionValue("brand"),t=e.getOptionValue("suffix"),a={};n&&(a.Environment=n),o&&(a.Brand=o),t&&(a.Suffix=t),p.populate(process.env,a,{override:!0}),process.env.TRACE&&console.log("set options",a)}function I(){if(process.env.TRACE&&console.log("loading branch..."),process.env.Branch){process.env.TRACE&&console.log("skip branch cause its defined already",process.env.Branch);return}const n={Branch:b.execSync("git rev-parse --abbrev-ref HEAD").toString("utf-8").replace(/[\n\r\s]+$/,"")};p.populate(process.env,n),process.env.TRACE&&console.log("set branch",n)}async function G(e){const n=process.env.MongoDbOptions__UserName;return n&&await e.removeUser(n),await e.dropDatabase(),{databaseName:e.databaseName,userName:n??""}}async function g(){if(!process.env.DefaultMigrationsDir)throw new Error("enviroment variable DefaultMigrationsDir is required");if(!process.env.Brand)throw new Error("enviroment variable Brand is required");const e=await E(),n=i.join(e,process.env.DefaultMigrationsDir),o=i.join(e,process.env.Brand),t=await K();await f(n,t),await f(o,t),await w(t)}async function U(e){if(e&&!process.env.DefaultMigrationsDir)throw new Error("enviroment variable DefaultMigrationsDir is required");if(!e&&!process.env.Brand)throw new Error("enviroment variable Brand is required");const n=e?process.env.DefaultMigrationsDir:process.env.Brand,o=await E(),t=i.join(o,n);await w(t)}async function K(){const e=await _();return await W(e),e}async function _(){const e=i.join(process.cwd(),".temp");return m.existsSync(e)?process.env.TRACE&&console.log("using tempDir",e):(m.mkdirSync(e),process.env.TRACE&&console.log("created tempDir",e)),e}async function W(e){const n=await l.readdir(e,{withFileTypes:!0});for(const o of n){const t=i.join(e,o.name);process.env.TRACE&&console.log("remove tempFile",t),await l.rm(t)}process.env.TRACE&&console.log("cleaned up tempDir",e)}async function w(e){const n=await s.config.read();n.migrationsDir=e,s.config.set(n),process.env.TRACE&&console.log("configured new migrationDir",e)}async function f(e,n){if(!m.existsSync(e)){process.env.TRACE&&console.log("sourceDir does not exist, skipping",e);return}let o=await l.readdir(e,{withFileTypes:!0});o=o.filter(t=>t.isFile());for(const t of o){const a=`${e}${i.sep}${t.name}`,c=`${n}${i.sep}${t.name}`;process.env.TRACE&&console.log("copy file",a,c),await l.copyFile(a,c)}}async function E(){const n=(await s.config.read()).migrationsDir;return i.isAbsolute(n)?n:i.join(process.cwd(),n)}const r=new h.Command;r.name("migrate-mongo").description("CLI to migrate mongodb").version(N.version).option("-e, --env <env>","set process.env.Environment").option("-b, --brand <brand>","set process.env.Brand").option("-s, --suffix <suffix>","set process.env.Suffix").option("-t, --trace","set process.env.TRACE to enable trace outputs");r.hook("preSubcommand",async e=>{process.env.TRACE=e.getOptionValue("trace")?"on":"",process.env.TRACE&&console.log("hook pre-command..."),await O(e)});r.command("create [description]").description("create a new database migration with the provided description").option("-d, --default","enforce creation in defaultMigration folder").action(async(e,n)=>{process.env.TRACE&&console.log("run command create...");try{await U(n.default);const o=await s.create(e),t=await s.config.read();console.log(`CREATED: ${t.migrationsDir}${i.sep}${o}`)}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}});r.command("up").description("run all pending database migrations").action(async()=>{process.env.TRACE&&console.log("run command up...");const{db:e,client:n}=await s.database.connect();try{await g(),(await s.up(e,n)).forEach(t=>console.log(`MIGRATED UP: ${t}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),o.migrated.forEach(t=>console.log(`MIGRATED UP: ${t}`)),process.exit(1)}finally{await n.close()}});r.command("down").description("undo the last applied database migration").action(async()=>{process.env.TRACE&&console.log("run command down...");const{db:e,client:n}=await s.database.connect();try{await g(),(await s.down(e,n)).forEach(t=>console.log(`MIGRATED DOWN: ${t}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});r.command("status").description("print the changelog of the database").action(async()=>{process.env.TRACE&&console.log("run command status...");const{db:e,client:n}=await s.database.connect();try{await g(),(await s.status(e)).forEach(t=>console.log(`${t.appliedAt}: ${t.fileName}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});r.command("dropDatabase").description("deletes the database").action(async()=>{process.env.TRACE&&console.log("run command dropDatabase...");const{db:e,client:n}=await s.database.connect();try{const o=await G(e);console.log("DROPPED DB:",o.databaseName,o.userName)}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});r.parse();
2
+ "use strict";const s=require("migrate-mongo"),h=require("commander"),p=require("dotenv"),v=require("dotenv-expand"),y=require("@samhammer/vault-client-sag"),d=require("lodash"),b=require("child_process"),i=require("path"),l=require("fs/promises"),m=require("fs"),D="@samhammer/migrate-mongo-sag",R="1.1.0",T={"migrate-mongo":"dist/migrate-mongo.js"},A=["dist/**/*"],C=["mongodb","migration"],$="SamhammerAG",k="MIT",q={build:"vite build","migrate-mongo":"vite build && node dist/migrate-mongo.js",format:"prettier --write --parser typescript ./src",lint:"eslint ./src",test:"vitest run"},M={"@rushstack/eslint-patch":"^1.10.3","@types/lodash":"^4.17.5","@types/migrate-mongo":"^8.2.0","@types/node":"^18.19.34","@typescript-eslint/eslint-plugin":"^7.13.0","@typescript-eslint/parser":"^7.13.0",eslint:"^8.57.0","eslint-config-prettier":"^8.10.0","eslint-plugin-import":"^2.29.1","eslint-plugin-prettier":"^5.1.3",mongodb:"^4.17.2",prettier:"^3.3.2",typescript:"^5.4.5",vite:"^5.2.13",vitest:"^1.6.0"},x={"@samhammer/vault-client-sag":"^1.1.1",commander:"^12.1.0",dotenv:"^16.4.5","dotenv-expand":"^11.0.6",lodash:"^4.17.21","migrate-mongo":"^11.0.0"},B={mongodb:"^4.4.1 || ^5.0.0 || ^6.0.0"},N={node:"18.19.0",yarn:"4.0.2"},O={name:D,version:R,bin:T,files:A,keywords:C,author:$,license:k,scripts:q,devDependencies:M,dependencies:x,peerDependencies:B,volta:N},f=[".env.local",".env"];async function S(e){try{process.env.TRACE&&console.log("int env..."),await P(e),await I(),await F(),await j(),process.env.TRACE&&console.log("finished init env")}catch(n){console.error("init env failed",n)}}function j(){process.env.TRACE&&console.log("loading env files...");const e=p.config({path:f});v.expand({parsed:e.parsed})}async function F(){process.env.TRACE&&console.log("loading vault...");const e=V(f);process.env.TRACE&&console.log("requesting vault keys",e),await(await y.getVault()).loadSecretsToEnv(e),process.env.TRACE&&console.log("finished loading from vault")}function V(e){const n=d.clone(process.env),o=p.config({path:e,processEnv:n}),t=d.pickBy(o.parsed,c=>c.startsWith("VaultKey")),a=v.expand({parsed:t,processEnv:n});return d.invert(a.parsed)}async function P(e){process.env.TRACE&&console.log("loading command options...");const n=e.getOptionValue("env"),o=e.getOptionValue("brand"),t=e.getOptionValue("app"),a={};n&&(a.Environment=n),o&&(a.Brand=o),t&&(a.App=t),p.populate(process.env,a,{override:!0}),process.env.TRACE&&console.log("set options",a)}function I(){if(process.env.TRACE&&console.log("loading branch..."),process.env.Branch){process.env.TRACE&&console.log("skip branch cause its defined already",process.env.Branch);return}const n={Branch:b.execSync("git rev-parse --abbrev-ref HEAD").toString("utf-8").replace(/[\n\r\s]+$/,"")};p.populate(process.env,n),process.env.TRACE&&console.log("set branch",n)}async function G(e){const n=process.env.MongoDbOptions__UserName;return n&&await e.removeUser(n),await e.dropDatabase(),{databaseName:e.databaseName,userName:n??""}}async function g(){if(!process.env.DefaultMigrationsDir)throw new Error("enviroment variable DefaultMigrationsDir is required");if(!process.env.Brand)throw new Error("enviroment variable Brand is required");const e=await E(),n=i.join(e,process.env.DefaultMigrationsDir),o=i.join(e,process.env.Brand),t=await K();await u(n,t),await u(o,t),await w(t)}async function U(e){if(e&&!process.env.DefaultMigrationsDir)throw new Error("enviroment variable DefaultMigrationsDir is required");if(!e&&!process.env.Brand)throw new Error("enviroment variable Brand is required");const n=e?process.env.DefaultMigrationsDir:process.env.Brand,o=await E(),t=i.join(o,n);await w(t)}async function K(){const e=await _();return await W(e),e}async function _(){const e=i.join(process.cwd(),".temp");return m.existsSync(e)?process.env.TRACE&&console.log("using tempDir",e):(m.mkdirSync(e),process.env.TRACE&&console.log("created tempDir",e)),e}async function W(e){const n=await l.readdir(e,{withFileTypes:!0});for(const o of n){const t=i.join(e,o.name);process.env.TRACE&&console.log("remove tempFile",t),await l.rm(t)}process.env.TRACE&&console.log("cleaned up tempDir",e)}async function w(e){const n=await s.config.read();n.migrationsDir=e,s.config.set(n),process.env.TRACE&&console.log("configured new migrationDir",e)}async function u(e,n){if(!m.existsSync(e)){process.env.TRACE&&console.log("sourceDir does not exist, skipping",e);return}let o=await l.readdir(e,{withFileTypes:!0});o=o.filter(t=>t.isFile());for(const t of o){const a=`${e}${i.sep}${t.name}`,c=`${n}${i.sep}${t.name}`;process.env.TRACE&&console.log("copy file",a,c),await l.copyFile(a,c)}}async function E(){const n=(await s.config.read()).migrationsDir;return i.isAbsolute(n)?n:i.join(process.cwd(),n)}const r=new h.Command;r.name("migrate-mongo").description("CLI to migrate mongodb").version(O.version).option("-e, --env <env>","set process.env.Environment").option("-b, --brand <brand>","set process.env.Brand").option("-a, --app <app>","set process.env.App").option("-t, --trace","set process.env.TRACE to enable trace outputs");r.hook("preSubcommand",async e=>{process.env.TRACE=e.getOptionValue("trace")?"on":"",process.env.TRACE&&console.log("hook pre-command..."),await S(e)});r.command("create [description]").description("create a new database migration with the provided description").option("-d, --default","enforce creation in defaultMigration folder").action(async(e,n)=>{process.env.TRACE&&console.log("run command create...");try{await U(n.default);const o=await s.create(e),t=await s.config.read();console.log(`CREATED: ${t.migrationsDir}${i.sep}${o}`)}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}});r.command("up").description("run all pending database migrations").action(async()=>{process.env.TRACE&&console.log("run command up...");const{db:e,client:n}=await s.database.connect();try{await g(),(await s.up(e,n)).forEach(t=>console.log(`MIGRATED UP: ${t}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),o.migrated.forEach(t=>console.log(`MIGRATED UP: ${t}`)),process.exit(1)}finally{await n.close()}});r.command("down").description("undo the last applied database migration").action(async()=>{process.env.TRACE&&console.log("run command down...");const{db:e,client:n}=await s.database.connect();try{await g(),(await s.down(e,n)).forEach(t=>console.log(`MIGRATED DOWN: ${t}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});r.command("status").description("print the changelog of the database").action(async()=>{process.env.TRACE&&console.log("run command status...");const{db:e,client:n}=await s.database.connect();try{await g(),(await s.status(e)).forEach(t=>console.log(`${t.appliedAt}: ${t.fileName}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});r.command("dropDatabase").description("deletes the database").action(async()=>{process.env.TRACE&&console.log("run command dropDatabase...");const{db:e,client:n}=await s.database.connect();try{const o=await G(e);console.log("DROPPED DB:",o.databaseName,o.userName)}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});r.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samhammer/migrate-mongo-sag",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "bin": {
5
5
  "migrate-mongo": "dist/migrate-mongo.js"
6
6
  },
@@ -38,7 +38,7 @@
38
38
  "vitest": "^1.6.0"
39
39
  },
40
40
  "dependencies": {
41
- "@samhammer/vault-client-sag": "^1.1.0",
41
+ "@samhammer/vault-client-sag": "^1.1.1",
42
42
  "commander": "^12.1.0",
43
43
  "dotenv": "^16.4.5",
44
44
  "dotenv-expand": "^11.0.6",