How to use strapi-utils - 10 common examples

To help you get started, we’ve selected a few strapi-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strapi / strapi / packages / strapi / bin / strapi.js View on Github external
program.version(packageJSON.version, '-v, --version');

// Make `-v` option case-insensitive.
process.argv = _.map(process.argv, arg => {
  return (arg === '-V') ? '-v' : arg;
});

// `$ strapi version` (--version synonym)
program
  .command('version')
  .description('output your version of Strapi')
  .action(() => { console.log(packageJSON.version); });


// `$ strapi console`
program
  .command('console')
  .description('open the Strapi framework console')
  .action(require('./strapi-console'));

// `$ strapi new`
program
  .command('new')
  .option('-d, --dev', 'Development mode')
  .option('--dbclient ', 'Database client')
  .option('--dbhost ', 'Database host')
  .option('--dbport ', 'Database port')
  .option('--dbname ', 'Database name')
  .option('--dbusername ', 'Database username')
  .option('--dbpassword ', 'Database password')
  .description('create a new application')
  .action(require('./strapi-new'));
github strapi / strapi / packages / strapi / bin / strapi-console.js View on Github external
strapi.start({}, err => {

    // Log and exit the REPL in case there is an error
    // while we were trying to start the server.
    if (err) {
      logger.error('Could not load the Strapi framework.');
      logger.error('Are you using the latest stable version?');
      process.exit(1);
    }

    // Open the Node.js REPL.
    const repl = REPL.start(strapi.config.name + ' > ' || 'strapi > ');
    repl.on('exit', err => {

      // Log and exit the REPL in case there is an error
      // while we were trying to open the REPL.
      if (err) {
        logger.error(err);
        process.exit(1);
      }

      process.exit(0);
github strapi / strapi / packages / strapi / bin / strapi-generate.js View on Github external
let invalidPackageJSON;

    try {
      require(pathToPackageJSON);
    } catch (e) {
      invalidPackageJSON = true;
    }

    if (invalidPackageJSON) {
      return logger.error('This command can only be used inside a Strapi project.');
    }
  }

  // Show usage if no generator type is defined.
  if (!scope.generatorType) {
    return logger.error('Write `$ strapi generate:something` instead.');
  }

  // Return the scope and the response (`error` or `success`).
  return generate(scope, {

    // Log and exit the REPL in case there is an error
    // while we were trying to generate the requested generator.
    error: function returnError(msg) {
      logger.error(msg);
      process.exit(1);
    },

    // Log and exit the REPL in case of success
    // but first make sure we have all the info we need.
    success: function returnSuccess() {
      if (!scope.outputPath && scope.filename && scope.destDir) {
github strapi / strapi / packages / strapi / bin / strapi-generate.js View on Github external
const scope = {
    rootPath: process.cwd(),
    strapiRoot: path.resolve(__dirname, '..'),
    id: id,
    args: cliArguments,
    strapiPackageJSON: packageJSON
  };

  // Register the generator type.
  // It can be a controller, model, service, etc.
  scope.generatorType = process.argv[2].split(':')[1];

  // Check that we're in a valid Strapi project.
  if (scope.generatorType !== 'new' || scope.generatorType !== 'generator' || scope.generatorType !== 'hook') {
    if (!cli.isStrapiApp()) {
      return logger.error('This command can only be used inside a Strapi project.');
    }
  }

  // Show usage if no generator type is defined.
  if (!scope.generatorType) {
    return logger.error('Write `$ strapi generate:something` instead.');
  }

  // Return the scope and the response (`error` or `success`).
  return generate(scope, {

    // Log and exit the REPL in case there is an error
    // while we were trying to generate the requested generator.
    error: function returnError(msg) {
      logger.error(msg);
      process.exit(1);
github strapi / strapi / packages / strapi / bin / strapi-uninstall.js View on Github external
module.exports = function (plugin) {
  // Define variables.
  const pluginPath = `./plugins/${plugin}`;

  // Check that we're in a valid Strapi project.
  if (!cli.isStrapiApp()) {
    return logger.error('This command can only be used inside a Strapi project.');
  }

  // Check that the plugin is installed.
  if (!fs.existsSync(pluginPath)) {
    logger.error(`It looks like this plugin is not installed. Please check that \`${pluginPath}\` folder exists.`);
    process.exit(1);
  }

  // Delete the plugin folder.
  rimraf(pluginPath, (err) => {
    if (err) {
      logger.error('An error occurred during plugin uninstallation.');
      process.exit(1);
    }

    // Success.
    logger.info('The plugin has been successfully uninstalled.');
    process.exit(0);
  });
};
github strapi / strapi / packages / strapi / bin / strapi-generate.js View on Github external
// Register the name.
  scope.generatorName = cliArguments[1];

  // Check that we're in a valid Strapi project.
  if (scope.generatorType !== 'new' || scope.generatorType !== 'generator' || scope.generatorType !== 'hook') {
    const pathToPackageJSON = path.resolve(scope.rootPath, 'package.json');
    let invalidPackageJSON;

    try {
      require(pathToPackageJSON);
    } catch (e) {
      invalidPackageJSON = true;
    }

    if (invalidPackageJSON) {
      return logger.error('This command can only be used inside a Strapi project.');
    }
  }

  // Show usage if no generator type is defined.
  if (!scope.generatorType) {
    return logger.error('Write `$ strapi generate:something` instead.');
  }

  // Return the scope and the response (`error` or `success`).
  return generate(scope, {

    // Log and exit the REPL in case there is an error
    // while we were trying to generate the requested generator.
    error: function returnError(msg) {
      logger.error(msg);
      process.exit(1);
github strapi / strapi / packages / strapi / lib / commands / generate.js View on Github external
success() {
      if (!scope.outputPath && scope.filename && scope.destDir) {
        scope.outputPath = scope.destDir + scope.filename;
      }

      if (scope.generatorType !== 'new') {
        logger.info(
          'Generated a new ' +
            scope.generatorType +
            ' `' +
            scope.humanizeId +
            '` at ' +
            scope.humanizedPath +
            '.'
        ); // eslint-disable-line prefer-template
      }

      process.exit(0);
    },
  });
github strapi / strapi / packages / strapi / bin / strapi-uninstall.js View on Github external
rimraf(pluginPath, (err) => {
    if (err) {
      logger.error('An error occurred during plugin uninstallation.');
      process.exit(1);
    }

    // Success.
    logger.info('The plugin has been successfully uninstalled.');
    process.exit(0);
  });
};
github strapi / strapi / packages / strapi / bin / strapi-generate.js View on Github external
success: function returnSuccess() {
      if (!scope.outputPath && scope.filename && scope.destDir) {
        scope.outputPath = scope.destDir + scope.filename;
      }

      if (scope.generatorType !== 'new') {
        logger.info('Generated a new ' + scope.generatorType + ' `' + scope.humanizeId + '` at ' + scope.humanizedPath + '.'); // eslint-disable-line prefer-template
      }

      process.exit(0);
    }
  });
github strapi / strapi / packages / strapi-plugin-graphql / services / Mutation.js View on Github external
const [name, action] = resolverOf.split('.');

      const controller = plugin
        ? _.get(
            strapi.plugins,
            `${plugin}.controllers.${_.toLower(name)}.${action}`
          )
        : _.get(strapi.controllers, `${_.toLower(name)}.${action}`);

      if (!controller) {
        return new Error(
          `Cannot find the controller's action ${name}.${action}`
        );
      }

      policiesFn[0] = policyUtils.globalPolicy(
        undefined,
        {
          handler: `${name}.${action}`,
        },
        undefined,
        plugin
      );
    }

    if (strapi.plugins['users-permissions']) {
      policies.unshift('plugins.users-permissions.permissions');
    }

    // Populate policies.
    policies.forEach(policy =>
      policyUtils.get(