Skip to content

Commit

Permalink
working refactor, make project:sync ignore more
Browse files Browse the repository at this point in the history
  • Loading branch information
jtoar committed Jun 9, 2023
1 parent 708902d commit 673fefe
Showing 1 changed file with 138 additions and 93 deletions.
231 changes: 138 additions & 93 deletions tasks/framework-tools/frameworkSyncToProject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,110 +20,155 @@ import {
copyFrameworkFilesToProject,
} from './lib/project.mjs'

const projectPath = process.argv?.[2] ?? process.env.RWJS_CWD
function main() {
const redwoodProjectPath = process.argv?.[2] ?? process.env.RWJS_CWD

if (!redwoodProjectPath) {
process.exitCode = 1
console.error([
'Error: Please specify the path to your Redwood project',
`Usage: ${process.argv?.[1]} /path/to/rw/project`,
])
return
}

// Cache the project's package.json and restore it when this process exits.
const redwoodProjectPackageJsonPath = path.join(
redwoodProjectPath,
'package.json'
)
const redwoodProjectPackageJson = fs.readFileSync(
redwoodProjectPackageJsonPath,
'utf-8'
)

process.on('SIGINT', () => {
console.log()
console.log(
`Removing framework packages from ${redwoodProjectPackageJsonPath}...`
)
fs.writeFileSync(redwoodProjectPackageJsonPath, redwoodProjectPackageJson)
console.log(
[
'',
'To get your project back to its original state...',
"- undo the changes to project's your yarn.lock file",
"- remove your project's node_modules directory",
"- run 'yarn install'",
].join('\n')
)

return
})

if (!projectPath) {
console.log('Error: Please specify the path to your Redwood Project')
console.log(`Usage: ${process.argv?.[1]} /path/to/rwjs/project`)
process.exit(1)
}
chokidar
.watch(REDWOOD_PACKAGES_PATH, {
ignoreInitial: true,
awaitWriteFinish: true,
ignored,
})
.on('ready', async () => {
logStatus('Cleaning Redwood framework...')
cleanPackages()

logStatus('Building Redwood framework...')
buildPackages()
console.log()

// Cache the original package.json and restore it when this process exits.
const projectPackageJsonPath = path.join(projectPath, 'package.json')
logStatus('Adding framework dependencies to project...')
addDependenciesToPackageJson(redwoodProjectPackageJsonPath)
installProjectPackages(redwoodProjectPath)
console.log()

const projectPackageJson = fs.readFileSync(projectPackageJsonPath, 'utf-8')
process.on('SIGINT', () => {
console.log()
console.log(`Removing framework packages from 'package.json'...`)
fs.writeFileSync(projectPackageJsonPath, projectPackageJson)
// TODO: Delete `node_modules/@redwoodjs`
console.log("...Done. Run 'yarn install'")
process.exit(0)
})
logStatus('Copying files...')
await copyFrameworkFilesToProject(redwoodProjectPath)
console.log()

function logStatus(m) {
console.log(c.bgYellow(c.black('rwfw ')), c.yellow(m))
logStatus('Waiting for changes')
console.log(separator)
})
.on('all', async (_event, filePath) => {
console.log({
event: _event,
filePath,
})
})
.on('all', async (_event, filePath) => {
logStatus(filePath)

if (filePath.endsWith('package.json')) {
logStatus(
`${c.red(
'Warning:'
)} You modified a package.json file. If you've modified the ${c.underline(
'dependencies'
)}, then you must run ${c.underline('yarn rwfw project:sync')} again.`
)
}

const packageJsonPath = resolvePackageJsonPath(filePath)
const packageName = packageJsonName(packageJsonPath)
logStatus(c.magenta(packageName))

let hasHadError = false

try {
console.log()
logStatus(`Cleaning ${packageName}...`)
cleanPackages([packageJsonPath])

console.log()
logStatus(`Building ${packageName}...`)
buildPackages([packageJsonPath])

console.log()
logStatus(`Copying ${packageName}...`)
await copyFrameworkFilesToProject(redwoodProjectPath, [packageJsonPath])
} catch (error) {
hasHadError = true
console.log(error)
console.log()
logError(`Error building ${packageName}...`)
}

if (!hasHadError) {
console.log()
logStatus(`Done, and waiting for changes...`)
console.log('-'.repeat(80))
}
})
}

function logError(m) {
console.log(c.bgRed(c.black('rwfw ')), c.red(m))
}
// TODO see if this matters for windows...
const ignored = [
/node_modules/,

chokidar
.watch(REDWOOD_PACKAGES_PATH, {
ignoreInitial: true,
persistent: true,
awaitWriteFinish: true,
ignored: (file) =>
file.includes('/node_modules/') ||
file.includes('/dist/') ||
file.includes('/dist') ||
file.includes('/__tests__/') ||
file.includes('/__fixtures__/') ||
file.includes('/.test./') ||
['.DS_Store'].some((ext) => file.endsWith(ext)),
})
.on('ready', async () => {
logStatus('Cleaning Framework...')
cleanPackages()
/dist/,

logStatus('Building Framework...')
buildPackages()
/__fixtures__/,
/__mocks__/,
/__tests__/,
/.test./,
/jest.config.{js,ts}/,

console.log()
logStatus('Adding dependencies...')
addDependenciesToPackageJson(projectPackageJsonPath)
installProjectPackages(projectPath)
/README.md/,

console.log()
logStatus('Copying files...')
await copyFrameworkFilesToProject(projectPath)
// esbuild emits meta.json files that we sometimes suffix.
/meta.(\w*\.?)json/,

console.log()
logStatus('Done, and waiting for changes...')
console.log('-'.repeat(80))
})
.on('all', async (_event, filePath) => {
logStatus(filePath)

if (filePath.endsWith('package.json')) {
logStatus(
`${c.red(
'Warning:'
)} You modified a package.json file. If you've modified the ${c.underline(
'dependencies'
)}, then you must run ${c.underline('yarn rwfw project:sync')} again.`
)
}

const packageJsonPath = resolvePackageJsonPath(filePath)
const packageName = packageJsonName(packageJsonPath)
logStatus(c.magenta(packageName))

let hasHadError = false

try {
console.log()
logStatus(`Cleaning ${packageName}...`)
cleanPackages([packageJsonPath])
(filePath) => IGNORE_EXTENSIONS.some((ext) => filePath.endsWith(ext)),
]

console.log()
logStatus(`Building ${packageName}...`)
buildPackages([packageJsonPath])
const IGNORE_EXTENSIONS = ['.DS_Store']

console.log()
logStatus(`Copying ${packageName}...`)
await copyFrameworkFilesToProject(projectPath, [packageJsonPath])
} catch (error) {
hasHadError = true
console.log(error)
console.log()
logError(`Error building ${packageName}...`)
}
function logStatus(m) {
console.log(c.bgYellow(c.black('rwfw ')), c.yellow(m))
}

if (!hasHadError) {
console.log()
logStatus(`Done, and waiting for changes...`)
console.log('-'.repeat(80))
}
})
function logError(m) {
console.log(c.bgRed(c.black('rwfw ')), c.red(m))
}

const separator = '-'.repeat(process.stdout.columns)

main()

0 comments on commit 673fefe

Please sign in to comment.