@samhammer/migrate-mongo-sag 0.0.2 → 0.0.3
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/dist/migrate-mongo.js +1 -1
- package/package.json +1 -1
package/dist/migrate-mongo.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
|
-
"use strict";const
|
|
2
|
+
"use strict";const t=require("migrate-mongo"),m=require("commander"),i=require("dotenv"),l=require("dotenv-expand"),g=require("@samhammer/vault-client-sag"),c=require("lodash"),u=require("child_process"),v="@samhammer/migrate-mongo-sag",f="0.0.3",E="dist/migrate-mongo.js",b={"migrate-mongo":"dist/migrate-mongo.js"},h=["dist/**/*"],y=["mongodb","migration"],D="SamhammerAG",w="MIT",B={build:"vite build","migrate-mongo":"vite build && npx .",format:"prettier --write --parser typescript ./src",lint:"eslint ./src",test:"vitest run"},G={"@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"},U={"@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"},R={node:"18.19.0",yarn:"4.0.2"},x={name:v,version:f,main:E,bin:b,files:h,keywords:y,author:D,license:w,scripts:B,devDependencies:G,dependencies:U,volta:R},d=[".env.local",".env"];async function k(e){try{process.env.DEBUG&&console.log("int env..."),await S(e),await V(),await $(),await N(),process.env.DEBUG&&console.log("finished init env")}catch(n){console.error("init env failed",n)}}function N(){process.env.DEBUG&&console.log("loading env files...");const e=i.config({path:d});l.expand({parsed:e.parsed})}async function $(){process.env.DEBUG&&console.log("loading vault...");const e=O(d);process.env.DEBUG&&console.log("requesting vault keys",e),await(await g.getVault()).loadSecretsToEnv(e),process.env.DEBUG&&console.log("finished loading from vault")}function O(e){const n=c.clone(process.env),o=i.config({path:e,processEnv:n}),s=c.pickBy(o.parsed,p=>p.startsWith("VaultKey")),r=l.expand({parsed:s,processEnv:n});return c.invert(r.parsed)}async function S(e){process.env.DEBUG&&console.log("loading command options...");const n=e.getOptionValue("env"),o=e.getOptionValue("brand"),s=e.getOptionValue("suffix"),r={};n&&(r.Environment=n),o&&(r.Brand=o),s&&(r.Suffix=s),i.populate(process.env,r,{override:!0}),process.env.DEBUG&&console.log("set options",r)}function V(){if(process.env.DEBUG&&console.log("loading branch..."),process.env.Branch){process.env.DEBUG&&console.log("skip branch cause its defined already",process.env.Branch);return}const n={Branch:u.execSync("git rev-parse --abbrev-ref HEAD").toString("utf-8").replace(/[\n\r\s]+$/,"")};i.populate(process.env,n),process.env.DEBUG&&console.log("set branch",n)}async function q(e){const n=process.env.MongoDbOptions__UserName;return n&&await e.removeUser(n),await e.dropDatabase(),{databaseName:e.databaseName,userName:n??""}}const a=new m.Command;a.name("migrate-mongo").description("CLI to migrate mongodb").version(x.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("-d, --debug","enable debug output");a.hook("preSubcommand",async e=>{process.env.DEBUG=e.getOptionValue("debug")?"on":"",process.env.DEBUG&&console.log("hook pre-command..."),await k(e)});a.command("create [description]").description("create a new database migration with the provided description").action(async e=>{process.env.DEBUG&&console.log("run command create...");try{const n=t.create(e),o=await t.config.read();console.log(`CREATED: ${o.migrationsDir}/${n}`)}catch(n){console.error(`ERROR: ${n.message}`,n.stack),process.exit(1)}});a.command("up").description("run all pending database migrations").action(async()=>{process.env.DEBUG&&console.log("run command up...");const{db:e,client:n}=await t.database.connect();try{(await t.up(e,n)).forEach(s=>console.log(`MIGRATED UP: ${s}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),o.migrated.forEach(s=>console.log(`MIGRATED UP: ${s}`)),process.exit(1)}finally{await n.close()}});a.command("down").description("undo the last applied database migration").action(async()=>{process.env.DEBUG&&console.log("run command down...");const{db:e,client:n}=await t.database.connect();try{(await t.down(e,n)).forEach(s=>console.log(`MIGRATED DOWN: ${s}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});a.command("status").description("print the changelog of the database").action(async()=>{process.env.DEBUG&&console.log("run command status...");const{db:e,client:n}=await t.database.connect();try{(await t.status(e)).forEach(s=>console.log(`${s.appliedAt}: ${s.fileName}`))}catch(o){console.error(`ERROR: ${o.message}`,o.stack),process.exit(1)}finally{await n.close()}});a.command("dropDatabase").description("deletes the database").action(async()=>{process.env.DEBUG&&console.log("run command dropDatabase...");const{db:e,client:n}=await t.database.connect();try{const o=await q(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()}});a.parse();
|