Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
fix: package management tool configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SidStraw authored and DanSnow committed Aug 9, 2023
1 parent dc03a6a commit 12ef94d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/karbon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"find-cache-dir": "^4.0.0",
"fs-extra": "^11.1.1",
"graphql": "^16.7.1",
"js-yaml": "^4.1.0",
"jszip": "^3.10.1",
"knitwork": "^1.0.0",
"lodash.truncate": "^4.4.2",
Expand Down Expand Up @@ -194,6 +195,7 @@
"@nuxtjs/tailwindcss": "6.8.0",
"@types/find-cache-dir": "3.2.1",
"@types/fs-extra": "11.0.1",
"@types/js-yaml": "^4.0.5",
"@types/jszip": "3.4.1",
"@types/lodash.truncate": "4.4.7",
"@types/minimist": "1.2.2",
Expand Down
37 changes: 25 additions & 12 deletions packages/karbon/src/cli/karbon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { globby } from 'globby'
import { oraPromise } from 'ora'
import parseArgv from 'minimist'
import chalk from 'chalk'
import { temporaryDirectoryTask, temporaryDirectory } from 'tempy'
import { temporaryDirectoryTask } from 'tempy'
import { $ } from 'execa'
import * as Sentry from '@sentry/node'
import consola from 'consola'
Expand All @@ -19,6 +19,7 @@ import { SummaryType } from './bundle/types'
import { bundleEditorBlocks, bundleLayouts } from './bundle'
import { checkFile } from './checkFile'
import { karbonMsg, sizeErrorMsg } from './checkFile/setting'
import { configYarn } from './prepare'

/* eslint-disable no-console */
const log = console.log
Expand All @@ -40,7 +41,7 @@ async function runBundle() {
try {
await oraPromise(compress, { text: 'compressing', successText: 'compressed' })
await checkFile(siteTemplateName)
await oraPromise(testBuild, { text: 'testing build', successText: 'tested build success' })
await oraPromise(testBuild, { text: 'testing', successText: 'tested' })
if (!argv.packOnly) {
await oraPromise(upload, { text: 'uploading', successText: 'uploaded' })
}
Expand Down Expand Up @@ -150,17 +151,29 @@ async function unzipFile(zipFilePath: string, targetDirectory: string) {
}
}

function getPackageManager(projectPath: string) {
const yarn = fs.existsSync(path.resolve(projectPath, './yarn.lock')) && 'yarn'
const npm = fs.existsSync(path.resolve(projectPath, './package-lock.json')) && 'npm'
const pnpm = fs.existsSync(path.resolve(projectPath, './pnpm-lock.yaml')) && 'pnpm'

return yarn || npm || pnpm || 'npm'
}

async function testBuild() {
const tempPath = temporaryDirectory()
log('====>>', { tempPath })

// const { exitCode, stderr } = await temporaryDirectoryTask(async (tempPath) => {
await unzipFile(siteTemplateName, tempPath)
const $$ = $({ cwd: tempPath })
await $$`yarn install`
const { exitCode, stderr } = await $$`yarn build`
return { exitCode, stderr }
// })
const { exitCode, stderr } = await temporaryDirectoryTask(async (tempPath) => {
await unzipFile(siteTemplateName, tempPath)
const $$ = $({ cwd: tempPath })

const packageManager = getPackageManager(tempPath)

if (packageManager === 'yarn') {
await configYarn(path.resolve(tempPath, './.yarnrc.yml'))
await $$`yarn set version berry`
}
await $$`${packageManager} install`
const { exitCode, stderr } = await $$`npx nuxi build`
return { exitCode, stderr }
})
if (exitCode !== 0) {
throw new Error(stderr)
}
Expand Down
31 changes: 31 additions & 0 deletions packages/karbon/src/cli/prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from 'fs-extra'
import yaml from 'js-yaml'
import invariant from 'tiny-invariant'

export async function configYarn(yarnConfigPath: string) {
const config = await loadYarnConfig(yarnConfigPath)

// use node-modules for compatible
config.nodeLinker = 'node-modules'

// make yarn only touch files under /tmp
config.enableGlobalCache = false
config.cacheFolder = '/tmp/home/.yarn-cache'

// remove custom yarn and plugins
Reflect.deleteProperty(config, 'yarnPath')
Reflect.deleteProperty(config, 'plugins')

await fs.writeFile(yarnConfigPath, yaml.dump(config))
}

async function loadYarnConfig(yarnConfigPath: string): Promise<Record<string, unknown>> {
try {
const content = await fs.readFile(yarnConfigPath, 'utf-8')
const config = yaml.load(content) || ({} as Record<string, unknown>)
invariant(config !== null && typeof config === 'object', 'YAML config invalid')
return config as unknown as Record<string, unknown>
} catch {
return {}
}
}
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2951,6 +2951,7 @@ __metadata:
"@storipress/vue-advertising": ^1.1.0
"@types/find-cache-dir": 3.2.1
"@types/fs-extra": 11.0.1
"@types/js-yaml": ^4.0.5
"@types/jszip": 3.4.1
"@types/lodash.truncate": 4.4.7
"@types/minimist": 1.2.2
Expand Down Expand Up @@ -2981,6 +2982,7 @@ __metadata:
fs-extra: 11.1.1
globby: 13.2.2
graphql: ^16.7.1
js-yaml: ^4.1.0
jszip: ^3.10.1
knitwork: ^1.0.0
lodash.truncate: ^4.4.2
Expand Down Expand Up @@ -3189,6 +3191,13 @@ __metadata:
languageName: node
linkType: hard

"@types/js-yaml@npm:^4.0.5":
version: 4.0.5
resolution: "@types/js-yaml@npm:4.0.5"
checksum: 7dcac8c50fec31643cc9d6444b5503239a861414cdfaa7ae9a38bc22597c4d850c4b8cec3d82d73b3fbca408348ce223b0408d598b32e094470dfffc6d486b4d
languageName: node
linkType: hard

"@types/json-schema@npm:^7.0.11, @types/json-schema@npm:^7.0.8, @types/json-schema@npm:^7.0.9":
version: 7.0.12
resolution: "@types/json-schema@npm:7.0.12"
Expand Down

0 comments on commit 12ef94d

Please sign in to comment.