Skip to content

Commit

Permalink
fix(github-actions): use chosen pkg manager
Browse files Browse the repository at this point in the history
  • Loading branch information
YassinEldeeb committed Apr 1, 2022
1 parent bd2bc47 commit 9462e37
Show file tree
Hide file tree
Showing 6 changed files with 1,631 additions and 102 deletions.
14 changes: 8 additions & 6 deletions packages/cpg-github-actions/template/.github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

$SETUP_PNPM

- uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
key: ${{ runner.os }}-modules-${{ hashFiles('**/$PKG_MANAGER_LOCK') }}

- name: Install dependencies
working-directory: $WORKING_DIR
run: yarn --frozen-lockfile
run: $INSTALL_CMD_WITH_FROZEN_LOCK

- name: Run Tests 🧪
working-directory: $WORKING_DIR
run: yarn test
run: $PKG_MANAGER_RUN_CMD test

Publish:
runs-on: ubuntu-latest
Expand All @@ -51,14 +53,14 @@ jobs:
- uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
key: ${{ runner.os }}-modules-${{ hashFiles('**/$PKG_MANAGER_LOCK') }}

- name: Install dependencies
working-directory: $WORKING_DIR
run: yarn install
run: $INSTALL_CMD_WITH_FROZEN_LOCK

- name: Publish 🚀
working-directory: $WORKING_DIR
run: yarn publish
run: $PKG_MANAGER_RUN_CMD publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
23 changes: 21 additions & 2 deletions packages/create-prisma-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execSync, spawnSync } from 'child_process'
import chalk from 'chalk'
import fs from 'fs'
import path from 'path'
import { runBlockingCommand } from './utils/runBlockingCommand'
import { runBlockingCommand } from './utils/getRunBlockingCommand'
import { promptQuestions } from './utils/inquirer/promptQuestions'
import { replacePlaceholders } from './utils/replacePlaceholders'
import { yarnWorkspaceJSON } from './config/yarn-workspace'
Expand All @@ -11,6 +11,8 @@ import { huskyCommitMsgHook } from './config/husky-commit-msg-hook'
import { CLIs } from './tinyClis'
import { getInstallCommand } from './utils/getInstallCommands'
import { transformScopedName } from './utils/transformScopedName'
import { getPkgManagerLockFile } from './utils/getPkgManagerLockFile'
import { getRunWithFrozenLockCMD } from './utils/runWithFrozenLockCMD'

export const main = async () => {
const answers = await promptQuestions()
Expand Down Expand Up @@ -78,14 +80,31 @@ export const main = async () => {

// Replace placeholders
const workflowPath = path.join(projectWorkdir, '.github/workflows/CI.yml')

let pkgManagerLockFile = getPkgManagerLockFile(pkgManager)
let runWithFrozenLockCMD = getRunWithFrozenLockCMD(pkgManager)
let runCMD = `${pkgManager} run`

fs.writeFileSync(
workflowPath,
fs
.readFileSync(workflowPath, 'utf-8')
.replace(
/\$WORKING_DIR/g,
usingWorkspaces ? './packages/generator' : '.',
),
)
.replace(
/\$SETUP_PNPM/,
pkgManager == 'pnpm'
? `- name: Setup PNPM
uses: pnpm/[email protected]
with:
version: 6.23.6`
: '',
)
.replace(/\$PKG_MANAGER_LOCK/g, pkgManagerLockFile)
.replace(/\$INSTALL_CMD_WITH_FROZEN_LOCK/g, runWithFrozenLockCMD)
.replace(/\$PKG_MANAGER_RUN_CMD/g, runCMD),
)
const dependabotPath = path.join(projectWorkdir, '.github/dependabot.yml')
fs.writeFileSync(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const getPkgManagerLockFile = (
pkgManager: 'npm' | 'yarn' | 'pnpm',
): string => {
let pkgManagerLockFile

switch (pkgManager) {
case 'npm':
pkgManagerLockFile = 'package-lock.json'
break
case 'yarn':
pkgManagerLockFile = 'yarn.lock'
break
case 'pnpm':
pkgManagerLockFile = 'pnpm-lock.yaml'
break
}

return pkgManagerLockFile
}
19 changes: 19 additions & 0 deletions packages/create-prisma-generator/src/utils/runWithFrozenLockCMD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const getRunWithFrozenLockCMD = (
pkgManager: 'npm' | 'yarn' | 'pnpm',
): string => {
let installWithFronzenLockCMD

switch (pkgManager) {
case 'npm':
installWithFronzenLockCMD = 'npm ci'
break
case 'yarn':
installWithFronzenLockCMD = 'yarn install --frozen-lockfile'
break
case 'pnpm':
installWithFronzenLockCMD = 'pnpm i --frozen-lockfile'
break
}

return installWithFronzenLockCMD
}
Loading

0 comments on commit 9462e37

Please sign in to comment.