Skip to content

Instantly share code, notes, and snippets.

@sandiprb
Created February 16, 2021 18:15
Show Gist options
  • Save sandiprb/69129b1056b7b4599b81d2d73a0b97fb to your computer and use it in GitHub Desktop.
Save sandiprb/69129b1056b7b4599b81d2d73a0b97fb to your computer and use it in GitHub Desktop.
Strapi App Script
const moment = require('moment')
const path = require('path');
const createStrapiApp = async projectPath => {
if (!projectPath) {
throw new Error(`
-> Path to strapi project is missing.
-> Usage: node migrate-3.4.0.js [path]`);
}
let strapi;
let app;
try {
strapi = require(require.resolve('strapi', { paths: [projectPath] }));
const pkgJSON = require(path.resolve(projectPath, 'package.json'));
if (!pkgJSON || !pkgJSON.dependencies || !pkgJSON.dependencies.strapi) {
throw new Error();
}
} catch (e) {
throw new Error(`
-> Strapi lib couldn\'t be found. Are the node_modules installed?
-> Fix: yarn install or npm install`);
}
try {
app = await strapi({ dir: projectPath }).load();
} catch (e) {
throw new Error(`
-> The migration couldn't be proceed because Strapi app couldn't start.
-> ${e.message}`);
}
return app;
};
async function main(){
const app = await createStrapiApp('./');
// run your logic below
// e.g. const articles = await app.query('articles').find()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment