@samhammer/migrate-mongo-sag 0.0.4 → 0.0.5

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.
@@ -1,3 +1,3 @@
1
1
  #! /usr/bin/env node
2
- "use strict";const s=require("migrate-mongo"),h=require("commander"),l=require("dotenv"),u=require("dotenv-expand"),E=require("@samhammer/vault-client-sag"),p=require("lodash"),b=require("child_process"),i=require("path"),y=require("os"),d=require("fs/promises"),R=require("fs"),D="@samhammer/migrate-mongo-sag",C="0.0.4",A={"migrate-mongo":"dist/migrate-mongo.js"},T=["dist/**/*"],$=["mongodb","migration"],x="SamhammerAG",M="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","@types/migrate-mongo":"^8.2.0","@types/node":"^18.0.0","@typescript-eslint/eslint-plugin":"^7.11.0","@typescript-eslint/parser":"^7.11.0",eslint:"^8.57.0","eslint-config-prettier":"^8.10.0","eslint-plugin-import":"^2.29.1","eslint-plugin-prettier":"^5.1.3",prettier:"^3.2.5",typescript:"^5.4.5",vite:"^5.2.12",vitest:"^1.6.0"},B={"@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":"^9.0.0",mongodb:"^4.17.2"},N={node:"18.19.0",yarn:"4.0.2"},O={name:D,version:C,bin:A,files:T,keywords:$,author:x,license:M,scripts:k,devDependencies:q,dependencies:B,volta:N},f=[".env.local",".env"];async function S(e){try{process.env.TRACE&&console.log("int env..."),await I(e),await P(),await j(),await V(),process.env.TRACE&&console.log("finished init env")}catch(n){console.error("init env failed",n)}}function V(){process.env.TRACE&&console.log("loading env files...");const e=l.config({path:f});u.expand({parsed:e.parsed})}async function j(){process.env.TRACE&&console.log("loading vault...");const e=F(f);process.env.TRACE&&console.log("requesting vault keys",e),await(await E.getVault()).loadSecretsToEnv(e),process.env.TRACE&&console.log("finished loading from vault")}function F(e){const n=p.clone(process.env),o=l.config({path:e,processEnv:n}),t=p.pickBy(o.parsed,c=>c.startsWith("VaultKey")),a=u.expand({parsed:t,processEnv:n});return p.invert(a.parsed)}async function I(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),l.populate(process.env,a,{override:!0}),process.env.TRACE&&console.log("set options",a)}function P(){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]+$/,"")};l.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.DefaultMigrations)throw new Error("enviroment variable DefaultMigrations is required");if(!process.env.Brand)throw new Error("enviroment variable Brand is required");const e=await w(),n=i.join(e,process.env.DefaultMigrations),o=i.join(e,process.env.Brand),t=await K();await m(n,t),await m(o,t),await v(t)}async function U(e){if(!process.env.DefaultMigrations)throw new Error("enviroment variable DefaultMigrations is required");const n=await w(),o=e||!process.env.Brand?process.env.DefaultMigrations:process.env.Brand,t=i.join(n,o);await v(t)}async function K(){const e=await d.mkdtemp(`${y.tmpdir()}${i.sep}`);return process.env.TRACE&&console.log("created tempDir",e),e}async function v(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 m(e,n){if(!R.existsSync(e)){process.env.TRACE&&console.log("sourceDir does not exist, skipping",e);return}let o=await d.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 d.copyFile(a,c)}}async function w(){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("-s, --suffix <suffix>","set process.env.Suffix").option("-t, --trace","set process.env.TRACE to enable trace ouputs");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
3
- if the brand is set, the migration will be placed in the brand folder, otherwise in the defaultMigration folder`).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"),E=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"),g=require("fs"),R="@samhammer/migrate-mongo-sag",D="0.0.5",T={"migrate-mongo":"dist/migrate-mongo.js"},C=["dist/**/*"],A=["mongodb","migration"],$="SamhammerAG",x="MIT",M={build:"vite build","migrate-mongo":"vite build && node dist/migrate-mongo.js",format:"prettier --write --parser typescript ./src",lint:"eslint ./src",test:"vitest run"},k={"@rushstack/eslint-patch":"^1.10.3","@types/lodash":"^4","@types/migrate-mongo":"^8.2.0","@types/node":"^18.0.0","@typescript-eslint/eslint-plugin":"^7.11.0","@typescript-eslint/parser":"^7.11.0",eslint:"^8.57.0","eslint-config-prettier":"^8.10.0","eslint-plugin-import":"^2.29.1","eslint-plugin-prettier":"^5.1.3",prettier:"^3.2.5",typescript:"^5.4.5",vite:"^5.2.12",vitest:"^1.6.0"},q={"@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":"^9.0.0",mongodb:"^4.17.2"},S={node:"18.19.0",yarn:"4.0.2"},B={name:R,version:D,bin:T,files:C,keywords:A,author:$,license:x,scripts:M,devDependencies:k,dependencies:q,volta:S},v=[".env.local",".env"];async function N(e){try{process.env.TRACE&&console.log("int env..."),await V(e),await P(),await j(),await O(),process.env.TRACE&&console.log("finished init env")}catch(n){console.error("init env failed",n)}}function O(){process.env.TRACE&&console.log("loading env files...");const e=p.config({path:v});u.expand({parsed:e.parsed})}async function j(){process.env.TRACE&&console.log("loading vault...");const e=F(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 F(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 V(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 P(){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 I(e){const n=process.env.MongoDbOptions__UserName;return n&&await e.removeUser(n),await e.dropDatabase(),{databaseName:e.databaseName,userName:n??""}}async function f(){if(!process.env.DefaultMigrations)throw new Error("enviroment variable DefaultMigrations is required");if(!process.env.Brand)throw new Error("enviroment variable Brand is required");const e=await h(),n=i.join(e,process.env.DefaultMigrations),o=i.join(e,process.env.Brand),t=await U();await m(n,t),await m(o,t),await w(t)}async function G(e){if(!process.env.DefaultMigrations)throw new Error("enviroment variable DefaultMigrations is required");const n=await h(),o=e||!process.env.Brand?process.env.DefaultMigrations:process.env.Brand,t=i.join(n,o);await w(t)}async function U(){const e=await K();return await _(e),e}async function K(){const e=i.join(process.cwd(),".temp");return g.existsSync(e)?process.env.TRACE&&console.log("using tempDir",e):(g.mkdirSync(e),process.env.TRACE&&console.log("created tempDir",e)),e}async function _(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 m(e,n){if(!g.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 h(){const n=(await s.config.read()).migrationsDir;return i.isAbsolute(n)?n:i.join(process.cwd(),n)}const r=new E.Command;r.name("migrate-mongo").description("CLI to migrate mongodb").version(B.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 ouputs");r.hook("preSubcommand",async e=>{process.env.TRACE=e.getOptionValue("trace")?"on":"",process.env.TRACE&&console.log("hook pre-command..."),await N(e)});r.command("create [description]").description(`create a new database migration with the provided description
3
+ if the brand is set, the migration will be placed in the brand folder, otherwise in the defaultMigration folder`).option("-d, --default","enforce creation in defaultMigration folder").action(async(e,n)=>{process.env.TRACE&&console.log("run command create...");try{await G(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 f(),(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 f(),(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 f(),(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 I(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": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "bin": {
5
5
  "migrate-mongo": "dist/migrate-mongo.js"
6
6
  },