Skip to content

Commit

Permalink
feat: optional outDir directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard committed Jan 2, 2023
1 parent b45b932 commit 13ef06e
Show file tree
Hide file tree
Showing 30 changed files with 448 additions and 289 deletions.
57 changes: 0 additions & 57 deletions assets/inject/gitignore

This file was deleted.

17 changes: 0 additions & 17 deletions assets/inject/package.config.template

This file was deleted.

10 changes: 0 additions & 10 deletions assets/inject/template-tsconfig.json

This file was deleted.

16 changes: 0 additions & 16 deletions assets/inject/template-tsconfig.lib.json

This file was deleted.

15 changes: 0 additions & 15 deletions assets/inject/template-tsconfig.settings.json

This file was deleted.

2 changes: 2 additions & 0 deletions src/actions/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sharedFlags from '../sharedFlags'
import {TypedFlags} from 'meow'
import {getPackage} from '../npm/package'
import {defaultSourceJs, defaultSourceTs} from '../configs/default-source'
import {defaultOutDir} from '../constants'

export const initFlags = {
...sharedFlags,
Expand Down Expand Up @@ -76,6 +77,7 @@ export async function init(options: InitOptions) {

await inject({
...options,
outDir: defaultOutDir,
requireUserConfirmation: !options.flags.force,
dependencies,
devDependencies,
Expand Down
146 changes: 60 additions & 86 deletions src/actions/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import {
} from '../util/files'
import {InitFlags} from './init'
import {PackageJson} from './verify/types'
import outdent from 'outdent'
import {injectPresets} from '../presets/presets'
import {tsconfigTemplateDist, tsconfigTemplate, tsconfigTemplateSettings} from '../configs/tsconfig'
import {pkgConfigTemplate} from '../configs/pkg-config'
import {gitignoreTemplate} from '../configs/git'
import {eslintignoreTemplate, eslintrcTemplate} from '../configs/eslint'

const bannedFields = ['login', 'description', 'projecturl', 'email']
const preferredLicenses = ['MIT', 'ISC', 'BSD-3-Clause']
Expand All @@ -31,7 +34,20 @@ const otherLicenses = Object.keys(licenses.list).filter((id) => {
)
})

export type FromTo = {from: string | string[]; to: string | string[]}
export interface InjectCopyFromTo {
type: 'copy'
from: string | string[]
to: string | string[]
}

export interface InjectTemplate {
type: 'template'
to: string | string[]
value: string
force?: boolean
}

export type Injectable = InjectCopyFromTo | InjectTemplate

export interface InjectOptions {
basePath: string
Expand All @@ -41,6 +57,7 @@ export interface InjectOptions {
devDependencies?: Record<string, string>
peerDependencies?: Record<string, string>
validate?: boolean
outDir: string
}

export interface PackageData {
Expand Down Expand Up @@ -111,11 +128,6 @@ async function injectBase(options: InjectOptions) {
didWrite = await writeStaticAssets(options)
info(didWrite.length > 0, 'Wrote static asset files: %s', didWrite.join(', '))

didWrite = await writeEslintrc(options)
info(didWrite, 'Wrote .eslintrc.js')
didWrite = await writeEslintIgnore(options)
info(didWrite, 'Wrote .eslintignore')

didWrite = await addBuildScripts(newPkg, options)
info(didWrite, 'Added build scripts to package.json')

Expand All @@ -140,62 +152,6 @@ async function writeReadme(data: PackageData, options: InjectOptions) {
return true
}

async function writeEslintrc(options: InjectOptions) {
if (!options.flags.eslint) {
return false
}
const {basePath} = options

const eslintrc = path.join(basePath, '.eslintrc')

const config = {
root: true,
env: {
node: true,
browser: true,
},
extends: [
'sanity',
options.flags.typescript && 'sanity/typescript',
'sanity/react',
'plugin:react-hooks/recommended',
options.flags.prettier && 'plugin:prettier/recommended',
].filter(Boolean),
}

const content = JSON.stringify(config, null, 2) + '\n'
await writeFileWithOverwritePrompt(eslintrc, content, {
encoding: 'utf8',
force: options.flags.force,
})
return true
}

async function writeEslintIgnore(options: InjectOptions) {
if (!options.flags.eslint) {
return false
}
const {basePath} = options

const eslintignore = path.join(basePath, '.eslintignore')

const content =
outdent`
.eslintrc.js
commitlint.config.js
lib
lint-staged.config.js
package.config.ts
${options.flags.typescript ? '*.js' : ''}
`.trim() + '\n'

await writeFileWithOverwritePrompt(eslintignore, content, {
encoding: 'utf8',
force: options.flags.force,
})
return true
}

async function writeLicense(
{license}: PackageData,
options: InjectOptions,
Expand Down Expand Up @@ -324,45 +280,63 @@ async function resolveProjectDescription(basePath: string, pkg: PackageJson | un
}
}

export async function writeAssets(files: FromTo[], {basePath, flags}: InjectOptions) {
export async function writeAssets(injectables: Injectable[], {basePath, flags}: InjectOptions) {
const assetsDir = await findAssetsDir()

const from = (...segments: string[]) => path.join(assetsDir, 'inject', ...segments)
const to = (...segments: string[]) => path.join(basePath, ...segments)

const writes: string[] = []
for (const file of files) {
const fromPath = asArray(file.from)
const toPath = asArray(file.to)
if (await copyFileWithOverwritePrompt(from(...fromPath), to(...toPath), flags)) {
for (const injectable of injectables) {
if (injectable.type === 'copy') {
const fromPath = asArray(injectable.from)
const toPath = asArray(injectable.to)
if (await copyFileWithOverwritePrompt(from(...fromPath), to(...toPath), flags)) {
writes.push(path.join(...toPath))
}
continue
}

if (injectable.type === 'template') {
const toPath = asArray(injectable.to)

await writeFileWithOverwritePrompt(to(...toPath), `${injectable.value.trim()}\n`, {
default: 'n',
force: injectable.force || flags.force,
})

writes.push(path.join(...toPath))
continue
}

throw new Error(`Unknown operation type "${(injectable as any).type}"`)
}

return writes
}

async function writeStaticAssets(options: InjectOptions) {
const {flags} = options

const files: FromTo[] = [
{from: 'editorconfig', to: '.editorconfig'},
{from: 'sanity.json', to: 'sanity.json'},
{from: 'v2-incompatible.js.template', to: 'v2-incompatible.js'},
{
from: 'package.config.template',
to: options.flags.typescript ? 'package.config.ts' : 'package.config.js',
},
flags.gitignore && {from: 'gitignore', to: '.gitignore'},
flags.typescript && {from: 'template-tsconfig.json', to: 'tsconfig.json'},
flags.typescript && {from: 'template-tsconfig.lib.json', to: 'tsconfig.lib.json'},
flags.typescript && {from: 'template-tsconfig.settings.json', to: 'tsconfig.settings.json'},
flags.prettier && {from: 'prettierrc.json', to: '.prettierrc.json'},
const {outDir, flags} = options

const files: Injectable[] = [
flags.eslint && eslintrcTemplate({flags: options.flags}),
flags.eslint && eslintignoreTemplate({outDir, flags: options.flags}),
{type: 'copy', from: 'editorconfig', to: '.editorconfig'},
{type: 'copy', from: 'sanity.json', to: 'sanity.json'},
{type: 'copy', from: 'v2-incompatible.js.template', to: 'v2-incompatible.js'},
pkgConfigTemplate({outDir, flags: options.flags}),
flags.gitignore && gitignoreTemplate(),
flags.typescript && tsconfigTemplate({flags: options.flags}),
flags.typescript && tsconfigTemplateDist({outDir, flags: options.flags}),
flags.typescript && tsconfigTemplateSettings({flags: options.flags}),
flags.prettier && {type: 'copy', from: 'prettierrc.json', to: '.prettierrc'},
]
.map((f) => (f ? (f as FromTo) : undefined))
.filter((f): f is FromTo => !!f)
.map((f) => (f ? (f as Injectable) : undefined))
.filter((f): f is Injectable => !!f)

const result = writeAssets(files, options)

return writeAssets(files, options)
return result
}

function asArray(input: string | string[]): string[] {
Expand Down
6 changes: 5 additions & 1 deletion src/actions/link-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import log from '../util/log'
import {getPackage} from '../npm/package'
import outdent from 'outdent'
import {fileExists, mkdir} from '../util/files'
import {loadConfig as loadPackageConfig} from '@sanity/pkg-utils'
import {defaultOutDir} from '../constants'

interface YalcWatchConfig {
folder?: string
Expand All @@ -42,8 +44,10 @@ export async function linkWatch({basePath}: {basePath: string}) {
fs.readFileSync(path.join(basePath, 'package.json'), 'utf8')
)

const packageConfig = await loadPackageConfig({cwd: basePath})

const watch: Required<YalcWatchConfig> = {
folder: 'lib',
folder: packageConfig?.dist ?? defaultOutDir,
command: 'npm run watch',
extensions: 'ts,js,png,svg,gif,jpeg,css',
...packageJson.sanityPlugin?.linkWatch,
Expand Down
7 changes: 4 additions & 3 deletions src/actions/verify-package.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {loadConfig as loadPackageConfig} from '@sanity/pkg-utils'
import {getPackage} from '../npm/package'
import log from '../util/log'
import {cliName, urls} from '../constants'
import {cliName, defaultOutDir, urls} from '../constants'
import {validateImports} from '../dependencies/import-linter'
import outdent from 'outdent'
import {
Expand Down Expand Up @@ -33,6 +33,7 @@ export async function verifyPackage({basePath, flags}: {basePath: string; flags:
const packageJson: PackageJson = await getPackage({basePath, validate: false})
const verifyConfig: VerifyPackageConfig = packageJson.sanityPlugin?.verifyPackage || {}
const packageConfig = await loadPackageConfig({cwd: basePath})
const outDir = packageConfig?.dist ?? defaultOutDir
const tsconfig = packageConfig?.tsconfig ?? 'tsconfig.json'

const validation = createValidator(verifyConfig, flags, errors)
Expand All @@ -43,11 +44,11 @@ export async function verifyPackage({basePath, flags}: {basePath: string; flags:
await validation('pkg-utils', async () => validatePkgUtilsDependency(packageJson))
await validation('srcIndex', async () => validateSrcIndexFile(basePath))
await validation('scripts', async () => validateScripts(packageJson))
await validation('module', async () => validateModule(packageJson))
await validation('module', async () => validateModule(packageJson, {outDir}))
await validation('nodeEngine', async () => validateNodeEngine(packageJson))

if (ts) {
await validation('tsconfig', async () => validateTsConfig(ts, {basePath}))
await validation('tsconfig', async () => validateTsConfig(ts, {basePath, outDir, tsconfig}))
}

await validation('sanityV2Json', async () => validatePluginSanityJson({basePath, packageJson}))
Expand Down
Loading

0 comments on commit 13ef06e

Please sign in to comment.