Created
January 28, 2023 07:00
-
-
Save monkey-company/689c6c6269b5d8e229b822b5adf6638f to your computer and use it in GitHub Desktop.
Strapi dev mode from js file without command for server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
// FIXME | |
/* eslint-disable import/extensions */ | |
const _ = require('lodash'); | |
const resolveCwd = require('resolve-cwd'); | |
const { yellow } = require('chalk'); | |
const { Command } = require('commander'); | |
const program = new Command(); | |
const packageJSON = require('./package.json'); | |
const checkCwdIsStrapiApp = (name) => { | |
const logErrorAndExit = () => { | |
console.log( | |
`You need to run ${yellow( | |
`strapi ${name}` | |
)} in a Strapi project. Make sure you are in the right directory.` | |
); | |
process.exit(1); | |
}; | |
try { | |
const pkgJSON = require(`${process.cwd()}/package.json`); | |
if (!_.has(pkgJSON, 'dependencies.@strapi/strapi')) { | |
logErrorAndExit(name); | |
} | |
} catch (err) { | |
logErrorAndExit(name); | |
} | |
}; | |
const getLocalScript = | |
(name) => | |
(...args) => { | |
checkCwdIsStrapiApp(name); | |
const cmdPath = resolveCwd.silent(`@strapi/strapi/lib/commands/${name}`); | |
if (!cmdPath) { | |
console.log( | |
`Error loading the local ${yellow( | |
name | |
)} command. Strapi might not be installed in your "node_modules". You may need to run "yarn install".` | |
); | |
process.exit(1); | |
} | |
const script = require(cmdPath); | |
Promise.resolve() | |
.then(() => { | |
return script(...args); | |
}) | |
.catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}); | |
}; | |
getLocalScript('develop')('--browser <name>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment