Skip to content

Commit

Permalink
feat: stabilish canary releases, fix broken installers (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoresende authored Jun 26, 2023
2 parents 3c7c122 + 70c2e8a commit cf0947d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-seals-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-cardinal-app": patch
---

(cli): rewrite scaffolding logic for broken installers
30 changes: 30 additions & 0 deletions .github/canary-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import fs from 'fs';
import { exec } from 'child_process';

const pkgJsonPaths = [
"cli/package.json"
]

try {
exec("git rev-parse --short HEAD", (err, stdout) => {
if (err) {
console.log(err);
process.exit(1);
}
const commitHash = stdout.trim();

for (const pkgJsonPath of pkgJsonPaths) {
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
const oldVersion = pkg.version;
const [major, minor, patch] = oldVersion.split(".").map(Number);
const newVersion = `${major}.${minor}.${patch + 1}-canary.${commitHash}`;
pkg.version = newVersion;
const content = JSON.stringify(pkg, null, "\t") + "\n";

fs.writeFileSync(pkgJsonPath, content);
}
});
} catch (error) {
console.error(error);
process.exit(1);
}
41 changes: 16 additions & 25 deletions .github/workflows/canary_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
branches:
- canary

permissions:
id-token: write

jobs:
release-canary:
if: ${{ github.repository_owner == 'ernesto-oss' }}
Expand Down Expand Up @@ -45,31 +48,19 @@ jobs:
- name: Build
run: pnpm turbo --filter=create-cardinal-app build

- name: Create Version PR or Publish Canary to NPM
id: changeset
uses: changesets/action@v1
with:
commit: "chore(release-canary): version packages"
title: "chore(release-canary): version packages"
version: "npx changeset version --snapshot canary"
publish: "npx changeset publish --no-git-tag --tag canary"
## The change made to package.json here is ephemeral, as it will only run in CI
- name: Bump version to canary
run: node .github/canary-release.js

- name: Install NPM
run: npm i -g [email protected] # need latest version for provenance

- name: Authenticate to npm and publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_ENV: "production"

- name: Get ID of latest Github release
id: releaseId
if: steps.changeset.outputs.published == 'true'
run: |
releaseId="$(gh release view --json id -R ernesto-oss/cardinal --jq '.id')"
echo 'RELEASE_ID='$releaseId >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Edit latest release to pre-release
if: steps.changeset.outputs.published == 'true'
uses: irongut/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
id: ${{ steps.releaseId.outputs.releaseId }}
pnpm build:cli
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
cd ./cli
npm publish --provenance --access public --ignore-scripts --tag canary
2 changes: 0 additions & 2 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# create-cardinal-app

## 0.2.1

### Patch Changes
Expand Down
19 changes: 12 additions & 7 deletions cli/src/installers/authInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
authDependencyMap,
type AvailableAuthDependenciesKeys,
} from "@/helpers/addPackageDependency.js";
import { removeArtifacts } from "@/helpers/removeArtifacts.js";
import { type ProjectOptions } from "@/index.js";

export const authInstaller = ({
Expand All @@ -23,13 +22,19 @@ export const authInstaller = ({
const authTemplateRoot = path.join(TEMPLATE_DIR, "auth");
const authDestination = path.join(projectDir, "packages/auth");

const copyDir = (fileName: string) =>
fs.copySync(path.join(authTemplateRoot, fileName), authDestination, {
filter: removeArtifacts,
});
const getTemplateTypeDirectory = () => {
let templateTypeDirectory = "";

if (databaseProvider === "planetscale" && frontendFramework === "next")
copyDir("auth-planetscale-next");
if (databaseProvider === "planetscale" && frontendFramework === "next")
templateTypeDirectory = "auth-planetscale-next";

return templateTypeDirectory;
};

fs.copySync(
path.join(authTemplateRoot, getTemplateTypeDirectory()),
path.join(authDestination),
);

/* Write `tsconfig.json` */
const templateAuthTsConfig = fs.readJsonSync(
Expand Down
25 changes: 14 additions & 11 deletions cli/src/installers/databaseInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
createPackageScripts,
databaseScriptsMap,
} from "@/helpers/createPackageScripts.js";
import { removeArtifacts } from "@/helpers/removeArtifacts.js";
import { type ProjectOptions } from "@/index.js";
import { type PackageManager } from "@/utils/getUserPackageManager.js";

Expand All @@ -31,17 +30,21 @@ export const databaseInstaller = ({
const databaseTemplateRoot = path.join(TEMPLATE_DIR, "database");
const databaseDestination = path.join(projectDir, "packages/database");

const copyDir = (fileName: string) =>
fs.copySync(
path.join(databaseTemplateRoot, fileName),
databaseDestination,
{ filter: removeArtifacts },
);
const getTemplateTypeDirectory = () => {
let templateTypeDirectory = "";

if (databaseProvider === "planetscale")
authentication
? copyDir("database-planetscale-auth")
: copyDir("database-planetscale");
if (databaseProvider === "planetscale")
authentication
? (templateTypeDirectory = "database-planetscale-auth")
: (templateTypeDirectory = "database-planetscale");

return templateTypeDirectory;
};

fs.copySync(
path.join(databaseTemplateRoot, getTemplateTypeDirectory()),
path.join(databaseDestination),
);

/* Write `tsconfig.json` */
const templateDatabaseTsConfig = fs.readJsonSync(
Expand Down
31 changes: 18 additions & 13 deletions cli/src/installers/graphqlInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
createPackageScripts,
graphQLScriptsMap,
} from "@/helpers/createPackageScripts.js";
import { removeArtifacts } from "@/helpers/removeArtifacts.js";
import { type ProjectOptions } from "@/index.js";
import { type PackageManager } from "@/utils/getUserPackageManager.js";

Expand All @@ -36,19 +35,25 @@ export const graphQLInstaller = ({
const graphqlTemplateRoot = path.join(TEMPLATE_DIR, "graphql");
const graphQLDestination = path.join(projectDir, "packages/api");

const copyDir = (fileName: string) =>
fs.copySync(path.join(graphqlTemplateRoot, fileName), graphQLDestination, {
filter: removeArtifacts,
});
const getTemplateTypeDirectory = () => {
let templateTypeDirectory = "";

if (frontendFramework === "next") {
databaseProvider === "planetscale" && authentication
? copyDir("graphql-next-planetscale-auth")
: null;
databaseProvider === "planetscale" && !authentication
? copyDir("graphql-next-planetscale")
: null;
}
if (frontendFramework === "next") {
databaseProvider === "planetscale" && authentication
? (templateTypeDirectory = "graphql-next-planetscale-auth")
: null;
databaseProvider === "planetscale" && !authentication
? (templateTypeDirectory = "graphql-next-planetscale")
: null;
}

return templateTypeDirectory;
};

fs.copySync(
path.join(graphqlTemplateRoot, getTemplateTypeDirectory()),
path.join(graphQLDestination),
);

/* Write `tsconfig.json` */
const templateGraphqlTsConfig = fs.readJsonSync(
Expand Down
20 changes: 12 additions & 8 deletions cli/src/installers/nextjsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
createPackageScripts,
nextScriptsMap,
} from "@/helpers/createPackageScripts.js";
import { removeArtifacts } from "@/helpers/removeArtifacts.js";
import { type ProjectOptions } from "@/index.js";
import { type PackageManager } from "@/utils/getUserPackageManager.js";

Expand All @@ -33,14 +32,19 @@ export const nextjsInstaller = ({
const nextTemplateRoot = path.join(TEMPLATE_DIR, "next");
const nextDestination = path.join(projectDir, "packages/web");

const copyDir = (fileName: string) =>
fs.copySync(path.join(nextTemplateRoot, fileName), nextDestination, {
filter: removeArtifacts,
});
const getTemplateTypeDirectory = () => {
if (backendType === "graphql") {
if (authentication) return "next-graphql-auth";
return "next-graphql";
}

if (backendType === "graphql") {
authentication ? copyDir("next-graphql-auth") : copyDir("next-graphql");
}
return "";
};

fs.copySync(
path.join(nextTemplateRoot, getTemplateTypeDirectory()),
path.join(nextDestination),
);

/* Write `tsconfig.json` */
const templateNextTsConfig = fs.readJsonSync(
Expand Down

0 comments on commit cf0947d

Please sign in to comment.