Skip to content

Commit

Permalink
feat(detection.js): we are now detecting a Strapi project
Browse files Browse the repository at this point in the history
We are now detecting a strapi project. If there is no project we are offerling to run the strapi cli
to create one.
  • Loading branch information
Simen Daehlin committed Dec 30, 2022
1 parent 5b15fb7 commit de5013f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
9 changes: 8 additions & 1 deletion index.js
Expand Up @@ -12,9 +12,12 @@ const questions = require(`./core/questions`);
const {
detectProjectType,
detectPackageManager,
detectStrapiProject,
goodbye,
detectDownloadsAndStars,
config
config,
createStrapiProject,
moveFiles
} = require(`./utils`);
const {
appendEnv,
Expand All @@ -37,6 +40,10 @@ const { clear, debug } = flags;
await detectDownloadsAndStars();
await detectProjectType();
await detectPackageManager();
if (!(await detectStrapiProject())) {
await createStrapiProject();
}

if (input.includes(`reset`)) {
await reset();
goodbye();
Expand Down
45 changes: 44 additions & 1 deletion utils/detection.js
Expand Up @@ -2,6 +2,7 @@ const path = require(`path`);
const { spinner, chalk, constants, access } = require(`./utils`);
const { setConfig, config } = require(`./config`);
const fetch = require(`node-fetch`);
const { readFile } = require(`fs`).promises;

const detectDownloadsAndStars = async () => {
spinner.start(` 🦄 ${chalk.yellow(`Prepping some magic`)} `);
Expand Down Expand Up @@ -86,8 +87,50 @@ const detectPackageManager = async () => {
}
};

const detectStrapiProject = async () => {
spinner.start(` 💻 Detecting Strapi project... `);
try {
const packageJson = await readFile(
path.join(process.cwd(), `package.json`),
`utf8`
);

const packageObj = JSON.parse(packageJson);
if (Object.prototype.hasOwnProperty.call(packageObj, `dependencies`)) {
if (
Object.prototype.hasOwnProperty.call(
packageObj.dependencies,
`@strapi/strapi`
)
) {
const strapiVersion = packageObj.dependencies[`@strapi/strapi`];
spinner.stopAndPersist({
symbol: `🚀`,
text: ` ${chalk.bold.yellowBright(
`Strapi ${strapiVersion}`
)} detected \n`
});
return true;
} else {
spinner.stopAndPersist({
symbol: `⛔️`,
text: ` ${chalk.bold.red(`Strapi project not detected`)} \n`
});
return false;
}
}
} catch (error) {
spinner.stopAndPersist({
symbol: `⛔️`,
text: ` ${chalk.bold.red(`Strapi project not detected`)} \n`
});
return false;
}
};

module.exports = {
detectPackageManager,
detectProjectType,
detectDownloadsAndStars
detectDownloadsAndStars,
detectStrapiProject
};
8 changes: 6 additions & 2 deletions utils/index.js
Expand Up @@ -12,10 +12,12 @@ const {
constants,
copyFile
} = require(`./utils`);
const { createStrapiProject } = require(`./createStrapi`);
const {
detectPackageManager,
detectProjectType,
detectDownloadsAndStars
detectDownloadsAndStars,
detectStrapiProject
} = require(`./detection`);

module.exports = {
Expand All @@ -32,7 +34,9 @@ module.exports = {
goodbye,
detectPackageManager,
detectProjectType,
detectStrapiProject,
setConfig,
config,
detectDownloadsAndStars
detectDownloadsAndStars,
createStrapiProject
};

0 comments on commit de5013f

Please sign in to comment.