Skip to content

Commit 457c904

Browse files
committed
fix(config): fn config load
closes #63
1 parent 0fa1ad4 commit 457c904

10 files changed

Lines changed: 119 additions & 79 deletions

File tree

.github/workflows/docs.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ jobs:
88
runs-on: ubuntu-latest
99
if: "!contains(github.event.head_commit.message, '[skip docs]')"
1010
steps:
11-
- uses: actions/checkout@v3
12-
- uses: pnpm/action-setup@v2
11+
- name: Checkout
12+
uses: actions/checkout@v3
1313
with:
14-
run_install: |
15-
- recursive: true
16-
args: [--frozen-lockfile, --strict-peer-dependencies]
17-
- run: pnpm build-docs
18-
- uses: peaceiris/actions-gh-pages@v3
14+
fetch-depth: 0
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: "18.x"
19+
- name: Install PNPM
20+
run: npm i -g pnpm
21+
- name: Install dependencies
22+
run: pnpm install --frozen-lockfile
23+
- name: Build Docs
24+
run: pnpm build-docs
25+
- name: Deploy on GitHub Pages
26+
uses: peaceiris/actions-gh-pages@v3
1927
with:
2028
github_token: ${{ secrets.GITHUB_TOKEN }}
2129
publish_dir: ./docs

.github/workflows/pull_requests.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ jobs:
88
runs-on: ubuntu-latest
99
if: "!contains(github.event.head_commit.message, '[skip ci]')"
1010
steps:
11-
- uses: actions/checkout@v3
12-
- uses: pnpm/action-setup@v2
11+
- name: Checkout
12+
uses: actions/checkout@v3
1313
with:
14-
run_install: |
15-
- recursive: true
16-
args: [--frozen-lockfile, --strict-peer-dependencies]
17-
- run: pnpm build
18-
- run: pnpm test
14+
fetch-depth: 0
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: "18.x"
19+
- name: Install PNPM
20+
run: npm i -g pnpm
21+
- name: Install dependencies
22+
run: pnpm install --frozen-lockfile
23+
- name: Run Tests
24+
run: pnpm test
25+
- name: Build Package
26+
run: pnpm build

.github/workflows/release.yml

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ name: Test & Build
22

33
on:
44
push:
5-
branches: [ master, develop, pre, feat/*, fix/* ]
5+
branches: [master, develop, pre, feat/*, fix/*]
6+
permissions:
7+
contents: read # for checkout
68
jobs:
79
test:
10+
name: Test
11+
permissions:
12+
contents: write # to be able to publish a GitHub release
13+
issues: write # to be able to comment on released issues
14+
pull-requests: write # to be able to comment on released pull requests
15+
id-token: write # to enable use of OIDC for npm provenance
816
runs-on: ubuntu-latest
9-
if: "!contains(github.event.head_commit.message, '[skip ci]')"
1017
steps:
1118
- uses: actions/checkout@v3
1219
- uses: pnpm/action-setup@v2
@@ -15,22 +22,36 @@ jobs:
1522
- recursive: true
1623
args: [--frozen-lockfile, --strict-peer-dependencies]
1724
- run: pnpm test
18-
if: "!contains(github.event.head_commit.message, '[skip test]')"
1925
build:
26+
name: Build
2027
runs-on: ubuntu-latest
21-
if: "!contains(github.event.head_commit.message, '[skip ci]')"
2228
needs: test
29+
permissions:
30+
contents: write # to be able to publish a GitHub release
31+
issues: write # to be able to comment on released issues
32+
pull-requests: write # to be able to comment on released pull requests
33+
id-token: write # to enable use of OIDC for npm provenance
2334
steps:
24-
- uses: actions/checkout@v3
25-
- uses: pnpm/action-setup@v2
35+
- name: Checkout
36+
uses: actions/checkout@v3
2637
with:
27-
run_install: |
28-
- recursive: true
29-
args: [--frozen-lockfile, --strict-peer-dependencies]
30-
- run: pnpm build
31-
- run: cd ./dist && pnpm pack --pack-destination=../
32-
- run: pnpm semantic-release
33-
if: "!contains(github.event.head_commit.message, '[skip publish]')"
38+
fetch-depth: 0
39+
- name: Setup Node.js
40+
uses: actions/setup-node@v3
41+
with:
42+
node-version: "18.x"
43+
- name: Install PNPM
44+
run: npm i -g pnpm
45+
- name: Install dependencies
46+
run: pnpm install --frozen-lockfile
47+
- name: Run Tests
48+
run: pnpm test
49+
- name: Build Package
50+
run: pnpm build
51+
- name: Pack
52+
run: cd ./dist && pnpm pack --pack-destination=../
53+
- name: Semantic Release
54+
run: npx semantic-release
3455
env:
3556
NPM_TOKEN: "${{ secrets.NPM_TOKEN }}"
3657
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
"date-fns": "^2.30.0",
4040
"glob": "^10.3.3",
4141
"handlebars": "^4.7.7",
42-
"massarg": "^1.0.7-pre.1"
42+
"massarg": "1.0.7-pre.1"
4343
},
4444
"devDependencies": {
4545
"@knodes/typedoc-plugin-pages": "^0.23.4",
4646
"@semantic-release/changelog": "^6.0.3",
47+
"@semantic-release/exec": "^6.0.3",
4748
"@semantic-release/git": "^10.0.1",
4849
"@semantic-release/release-notes-generator": "^10.0.3",
4950
"@types/jest": "^29.5.1",

scaffold.config.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/** @type {import('simple-scaffold').ScaffoldConfigFile} */
2-
module.exports = {
3-
default: {
4-
templates: ["examples/test-input/Component"],
5-
output: "examples/test-output",
6-
data: { property: "myProp", value: "10" },
7-
},
8-
component: {
9-
templates: ["examples/test-input/Component"],
10-
output: "examples/test-output/component",
11-
data: { property: "myProp", value: "10" },
12-
},
2+
module.exports = (conf) => {
3+
console.log("Config:", conf)
4+
return {
5+
default: {
6+
templates: ["examples/test-input/Component"],
7+
output: "examples/test-output",
8+
data: { property: "myProp", value: "10" },
9+
},
10+
component: {
11+
templates: ["examples/test-input/Component"],
12+
output: "examples/test-output/component",
13+
data: { property: "myProp", value: "10" },
14+
},
15+
}
1316
}

src/cmd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { LogLevel, ScaffoldCmdConfig } from "./types"
55
import { Scaffold } from "./scaffold"
66
import path from "node:path"
77
import fs from "node:fs/promises"
8-
import { parseAppendData, parseConfig } from "./config"
8+
import { parseAppendData, parseConfigFile } from "./config"
99

1010
export async function parseCliArgs(args = process.argv.slice(2)) {
1111
const pkgFile = await fs.readFile(path.join(__dirname, "package.json"))
@@ -16,7 +16,7 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
1616
return (
1717
massarg<ScaffoldCmdConfig>()
1818
.main(async (config) => {
19-
const _config = await parseConfig(config)
19+
const _config = await parseConfigFile(config)
2020
return Scaffold(_config)
2121
})
2222
.option({

src/config.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ScaffoldConfig,
99
ScaffoldConfigFile,
1010
} from "./types"
11-
import { OptionsBase } from "massarg/types"
1211
import { handlebarsParse } from "./parser"
1312
import { log } from "./logger"
1413
import { resolve, wrapNoopResolver } from "./utils"
@@ -30,7 +29,7 @@ export function getOptionValueForFile<T>(
3029
)
3130
}
3231

33-
export function parseAppendData(value: string, options: ScaffoldCmdConfig & OptionsBase): unknown {
32+
export function parseAppendData(value: string, options: ScaffoldCmdConfig): unknown {
3433
const data = options.data ?? {}
3534
const [key, val] = value.split(/\:?=/)
3635
// raw
@@ -45,7 +44,7 @@ function isWrappedWithQuotes(string: string): boolean {
4544
}
4645

4746
/** @internal */
48-
export async function parseConfig(config: ScaffoldCmdConfig & OptionsBase): Promise<ScaffoldConfig> {
47+
export async function parseConfigFile(config: ScaffoldCmdConfig): Promise<ScaffoldConfig> {
4948
let c: ScaffoldConfig = config
5049
if (config.github) {
5150
log(config, LogLevel.Info, `Loading config from github ${config.github}`)
@@ -61,16 +60,19 @@ export async function parseConfig(config: ScaffoldCmdConfig & OptionsBase): Prom
6160
quiet: config.quiet,
6261
verbose: config.verbose,
6362
})
64-
const configImport = await resolve(configPromise, config)
65-
63+
let configImport = await resolve(configPromise, config)
64+
if (typeof configImport.default === "function" || configImport.default instanceof Promise) {
65+
configImport = await resolve(configImport.default, config)
66+
}
6667
if (!configImport[key]) {
6768
throw new Error(`Template "${key}" not found in ${configFile}`)
6869
}
70+
const importedKey = configImport[key]
6971
c = {
7072
...config,
71-
...configImport[key],
73+
...importedKey,
7274
data: {
73-
...configImport[key].data,
75+
...(importedKey as any).data,
7476
...config.data,
7577
},
7678
}

src/git.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from "node:path"
22
import os from "node:os"
33
import { log } from "./logger"
4-
import { AsyncResolver, LogConfig, LogLevel, ScaffoldCmdConfig, ScaffoldConfigMap } from "./types"
4+
import { AsyncResolver, LogConfig, LogLevel, ScaffoldCmdConfig, ScaffoldConfig, ScaffoldConfigMap } from "./types"
55
import { spawn } from "node:child_process"
6-
import { wrapNoopResolver } from "./utils"
6+
import { resolve, wrapNoopResolver } from "./utils"
77

88
export async function getGitConfig(
99
url: URL,
@@ -15,7 +15,7 @@ export async function getGitConfig(
1515

1616
const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`)
1717

18-
return new Promise((resolve, reject) => {
18+
return new Promise((res, reject) => {
1919
const clone = spawn("git", ["clone", "--depth", "1", repoUrl, tmpPath])
2020

2121
clone.on("error", reject)
@@ -24,18 +24,21 @@ export async function getGitConfig(
2424
log(logConfig, LogLevel.Info, `Loading config from git repo: ${repoUrl}`)
2525
const hashPath = url.hash?.replace("#", "") || "scaffold.config.js"
2626
const absolutePath = path.resolve(tmpPath, hashPath)
27-
const loadedConfig = (await import(absolutePath)).default as ScaffoldConfigMap
28-
log(logConfig, LogLevel.Info, `Loaded config from git`)
29-
log(logConfig, LogLevel.Debug, `Raw config:`, loadedConfig)
30-
const fixedConfig: ScaffoldConfigMap = Object.fromEntries(
31-
Object.entries(loadedConfig).map(([k, v]) => [
32-
k,
33-
// use absolute paths for template as config is necessarily in another directory
34-
{ ...v, templates: v.templates.map((t) => path.resolve(tmpPath, t)) },
35-
]),
27+
const loadedConfig = await resolve(
28+
async () => (await import(absolutePath)).default as ScaffoldConfigMap,
29+
logConfig,
3630
)
3731

38-
resolve(wrapNoopResolver(fixedConfig))
32+
log(logConfig, LogLevel.Info, `Loaded config from git`)
33+
log(logConfig, LogLevel.Debug, `Raw config:`, loadedConfig)
34+
const fixedConfig: ScaffoldConfigMap = {}
35+
for (const [k, v] of Object.entries(loadedConfig)) {
36+
fixedConfig[k] = {
37+
...v,
38+
templates: v.templates.map((t) => path.resolve(tmpPath, t)),
39+
}
40+
}
41+
res(wrapNoopResolver(fixedConfig))
3942
return
4043
}
4144

src/scaffold.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import {
1616
handleTemplateFile,
1717
} from "./file"
1818
import { LogLevel, MinimalConfig, Resolver, ScaffoldCmdConfig, ScaffoldConfig } from "./types"
19-
import { OptionsBase } from "massarg/types"
2019
import { defaultHelpers, registerHelpers } from "./parser"
2120
import { log, logInitStep, logInputFile } from "./logger"
22-
import { parseConfig } from "./config"
21+
import { parseConfigFile } from "./config"
2322

2423
/**
2524
* Create a scaffold using given `options`.
@@ -110,29 +109,27 @@ export async function Scaffold(config: ScaffoldConfig): Promise<void> {
110109
* @category Main
111110
* @return {Promise<void>} A promise that resolves when the scaffold is complete
112111
*/
113-
Scaffold.fromConfig = async function (
112+
Scaffold.fromConfig = async function(
114113
/** The path or URL to the config file */
115114
pathOrUrl: string,
116115
/** Information needed before loading the config */
117116
config: MinimalConfig,
118117
/** Any overrides to the loaded config */
119118
overrides?: Resolver<ScaffoldCmdConfig, Partial<Omit<ScaffoldConfig, "name">>>,
120119
): Promise<void> {
121-
const _cmdConfig: ScaffoldCmdConfig & OptionsBase = {
120+
const _cmdConfig: ScaffoldCmdConfig = {
122121
dryRun: false,
123122
output: process.cwd(),
124123
verbose: LogLevel.Info,
125124
overwrite: false,
126125
templates: [],
127126
createSubFolder: false,
128127
quiet: false,
129-
help: false,
130-
extras: [],
131128
config: pathOrUrl,
132129
...config,
133130
}
134131
const _overrides = resolve(overrides, _cmdConfig)
135-
const _config = await parseConfig(_cmdConfig)
132+
const _config = await parseConfigFile(_cmdConfig)
136133
return Scaffold({ ..._config, ..._overrides })
137134
}
138135

0 commit comments

Comments
 (0)