Skip to content

Commit

Permalink
feat: add support for dropping the database (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvincrespo authored Oct 2, 2020
1 parent 11428a2 commit b0d8424
Show file tree
Hide file tree
Showing 10 changed files with 1,820 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
- name: Install packages
run: |
yarn install --frozen-lockfile
- name: Run Unit Tests
run: |
yarn test:unit
- name: Run Cypress E2E Tests
uses: cypress-io/github-action@v2
with:
Expand All @@ -41,7 +44,6 @@ jobs:
env:
NODE_ENV: test
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ cypress/screenshots*
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn-error.log*

tmp
1 change: 1 addition & 0 deletions cypress/integration/createBisonAppWithDefaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ describe("Creating a new app", () => {
});
});
});

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"scripts": {
"semantic-release": "semantic-release",
"test:e2e": "start-server-and-test 'node ./scripts/createAppAndStartServer foo --acceptDefaults' 3001 'cypress run --spec cypress/integration/createBisonAppWithDefaults.test.js'"
"test:e2e": "start-server-and-test 'node ./scripts/createAppAndStartServer foo --acceptDefaults' 3001 'cypress run --spec cypress/integration/createBisonAppWithDefaults.test.js'",
"test:unit": "node ./scripts/createApp foo --acceptDefaults 3001 && yarn jest"
},
"files": [
"logo.js",
Expand Down Expand Up @@ -44,6 +45,7 @@
"@semantic-release/git": "^9.0.0",
"cypress": "^5.1.0",
"ejs-lint": "^1.1.0",
"jest": "^26.4.2",
"prettier": "^2.0.5",
"semantic-release": "^17.1.1",
"start-server-and-test": "^1.11.3"
Expand All @@ -70,5 +72,8 @@
],
"@semantic-release/github"
]
},
"jest": {
"testPathIgnorePatterns": ["node_modules", "cypress", "template"]
}
}
49 changes: 49 additions & 0 deletions scripts/createApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { promisify } = require("util");
const fs = require("fs");
const os = require("os");
const path = require("path");
const mkdtemp = promisify(fs.mkdtemp);
const execa = require("execa");
module.exports = {};

async function init() {
const args = process.argv.slice(2);
return await createApp(args);
}

async function createTmpDir() {
const tmpDir = await mkdtemp(`${os.tmpdir()}${path.sep}`);

if (!fs.existsSync("./tmp")) {
await fs.promises.mkdir("./tmp");
}

fs.writeFile("./tmp/tmpDir", tmpDir, () => {
console.log("tmpDir location stored.");
});

return tmpDir;
}

async function createApp(args) {
console.log("my args are", args);
const tmpdir = await createTmpDir();
const cliPath = path.join(__dirname, "..", "cli.js");

const cliOptions = args.length ? args : ["myapp", "--acceptDefaults"];
const name = cliOptions.splice(0, 1)[0];

console.log("cli options are", cliOptions);
console.log("cli path", cliPath);
console.log("installing to:", tmpdir);
console.log("nodeargs", [cliPath, name, ...cliOptions]);

await execa("node", [cliPath, name, ...cliOptions], {
cwd: tmpdir,
stdio: "inherit",
});

return path.join(tmpdir, name);
}

init();
23 changes: 19 additions & 4 deletions scripts/createAppAndStartServer.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
const { promisify } = require("util");
const fs = require("fs");
const os = require("os");
const path = require("path");
const mkdtemp = promisify(require("fs").mkdtemp);
const mkdtemp = promisify(fs.mkdtemp);
const execa = require("execa");
module.exports = {};

async function createAppAndStartServer() {
async function init() {
const args = process.argv.slice(2);
const appDir = await createApp(args);
return startServer(appDir);
}

async function createTmpDir() {
const tmpDir = await mkdtemp(`${os.tmpdir()}${path.sep}`);

if (!fs.existsSync("./tmp")) {
await fs.promises.mkdir("./tmp");
}

fs.writeFile("./tmp/tmpDir", tmpDir, () => {
console.log("tmpDir location stored.");
});

return tmpDir;
}

async function createApp(args) {
console.log("my args are", args);
const tmpdir = await mkdtemp(`${os.tmpdir()}${path.sep}`);
const tmpdir = await createTmpDir();
const cliPath = path.join(__dirname, "..", "cli.js");

const cliOptions = args.length ? args : ["myapp", "--acceptDefaults"];
Expand All @@ -40,4 +55,4 @@ function startServer(cwd) {
});
}

createAppAndStartServer();
init();
1 change: 1 addition & 0 deletions template/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"cypress:local": "CYPRESS_LOCAL=true CYPRESS_BASE_URL=http://localhost:3000 cypress",
"db:migrate": "yarn -s prisma migrate up --experimental",
"db:setup": "yarn db:migrate && yarn prisma generate",
"db:drop": "DOTENV_CONFIG_PATH=./prisma/.env ts-node -r dotenv/config ./scripts/dropDatabase",
"dev": "concurrently -n \"WATCHERS,NEXT\" -c \"black.bgYellow.dim,black.bgCyan.dim\" \"yarn watch:all\" \"next dev\"",
"g:cell": "hygen cell new --name",
"g:component": "hygen component new --name",
Expand Down
24 changes: 24 additions & 0 deletions template/scripts/dropDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export {};

const url = require('url');
const Client = require('pg').Client;

const connectionString = process.env.DATABASE_URL;
const { auth: user, hostname: host, port, path } = url.parse(connectionString);
const db = new Client({ user, host, port });
const dbName = path.slice(1).substring(0, path.slice(1).lastIndexOf('?'));

async function drop() {
await db.connect();
console.log(`dropping the ${dbName} database`);

try {
await db.query(`DROP DATABASE "${dbName}"`);
} catch (error) {
console.log({ error });
}

await db.end();
}

drop();
28 changes: 28 additions & 0 deletions test/prisma.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { promisify } = require("util");
const fs = require("fs");

async function getTmpDir() {
const readFile = promisify(fs.readFile);

const tmpDir = await readFile("./tmp/tmpDir");

return tmpDir;
}

beforeEach(async () => {
const path = await getTmpDir();

require("dotenv").config({
path: `${path}/foo/prisma/.env`,
});
});

describe("Prisma", () => {
describe("Environment Variables", () => {
it("correctly defines a DATABASE_URL env var", () => {
expect(process.env.DATABASE_URL).toBe(
"postgresql://postgres@localhost:5432/foo_dev?schema=public"
);
});
});
});
Loading

0 comments on commit b0d8424

Please sign in to comment.