Merge pull request #13187 from strapi/typescript/fix-cli

Typescript/fix cli
This commit is contained in:
Jean-Sébastien Herbaux 2022-05-02 15:20:21 +02:00 committed by GitHub
commit 8c5bbefaa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 88 additions and 12 deletions

View File

@ -95,6 +95,7 @@ program
.option('--dbssl <dbssl>', 'Database SSL') .option('--dbssl <dbssl>', 'Database SSL')
.option('--dbfile <dbfile>', 'Database file path for sqlite') .option('--dbfile <dbfile>', 'Database file path for sqlite')
.option('--dbforce', 'Allow overwriting existing database content') .option('--dbforce', 'Allow overwriting existing database content')
.option('-ts, --typescript', 'Create a typescript project')
.description('Create a new application') .description('Create a new application')
.action(require('../lib/commands/new')); .action(require('../lib/commands/new'));

View File

@ -1,8 +1,10 @@
'use strict'; 'use strict';
const path = require('path');
const { yup } = require('@strapi/utils'); const { yup } = require('@strapi/utils');
const _ = require('lodash'); const _ = require('lodash');
const inquirer = require('inquirer'); const inquirer = require('inquirer');
const tsUtils = require('@strapi/typescript-utils');
const strapi = require('../index'); const strapi = require('../index');
const emailValidator = yup const emailValidator = yup
@ -90,7 +92,12 @@ module.exports = async function(cmdOptions = {}) {
}; };
async function createAdmin({ email, password, firstname, lastname }) { async function createAdmin({ email, password, firstname, lastname }) {
const app = await strapi().load(); const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir);
const distDir = isTSProject ? path.join(appDir, 'dist') : appDir;
const app = await strapi({ appDir, distDir }).load();
const user = await app.admin.services.user.exists({ email }); const user = await app.admin.services.user.exists({ email });

View File

@ -1,7 +1,9 @@
'use strict'; 'use strict';
const path = require('path');
const _ = require('lodash'); const _ = require('lodash');
const inquirer = require('inquirer'); const inquirer = require('inquirer');
const tsUtils = require('@strapi/typescript-utils');
const strapi = require('../index'); const strapi = require('../index');
const promptQuestions = [ const promptQuestions = [
@ -42,7 +44,12 @@ module.exports = async function(cmdOptions = {}) {
}; };
async function changePassword({ email, password }) { async function changePassword({ email, password }) {
const app = await strapi().load(); const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir);
const distDir = isTSProject ? path.join(appDir, 'dist') : appDir;
const app = await strapi({ appDir, distDir }).load();
await app.admin.services.user.resetPasswordByEmail(email, password); await app.admin.services.user.resetPasswordByEmail(email, password);

View File

@ -1,6 +1,8 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('fs');
const path = require('path');
const tsUtils = require('@strapi/typescript-utils');
const strapi = require('../index'); const strapi = require('../index');
const CHUNK_SIZE = 100; const CHUNK_SIZE = 100;
@ -12,7 +14,12 @@ const CHUNK_SIZE = 100;
module.exports = async function({ file: filePath, pretty }) { module.exports = async function({ file: filePath, pretty }) {
const output = filePath ? fs.createWriteStream(filePath) : process.stdout; const output = filePath ? fs.createWriteStream(filePath) : process.stdout;
const app = await strapi().load(); const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir);
const distDir = isTSProject ? path.join(appDir, 'dist') : appDir;
const app = await strapi({ appDir, distDir }).load();
const count = await app.query('strapi::core-store').count(); const count = await app.query('strapi::core-store').count();

View File

@ -1,7 +1,10 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('fs');
const path = require('path');
const _ = require('lodash'); const _ = require('lodash');
const tsUtils = require('@strapi/typescript-utils');
const strapi = require('../index'); const strapi = require('../index');
/** /**
@ -12,7 +15,12 @@ const strapi = require('../index');
module.exports = async function({ file: filePath, strategy = 'replace' }) { module.exports = async function({ file: filePath, strategy = 'replace' }) {
const input = filePath ? fs.readFileSync(filePath) : await readStdin(process.stdin); const input = filePath ? fs.readFileSync(filePath) : await readStdin(process.stdin);
const app = await strapi().load(); const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir);
const distDir = isTSProject ? path.join(appDir, 'dist') : appDir;
const app = await strapi({ appDir, distDir }).load();
let dataToImport; let dataToImport;
try { try {

View File

@ -1,14 +1,22 @@
'use strict'; 'use strict';
const REPL = require('repl'); const REPL = require('repl');
const path = require('path');
const tsUtils = require('@strapi/typescript-utils');
const strapi = require('../index'); const strapi = require('../index');
/** /**
* `$ strapi console` * `$ strapi console`
*/ */
module.exports = () => { module.exports = async () => {
// Now load up the Strapi framework for real. // Now load up the Strapi framework for real.
const app = strapi(); const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir);
const distDir = isTSProject ? path.join(appDir, 'dist') : appDir;
const app = await strapi({ appDir, distDir }).load();
app.start().then(() => { app.start().then(() => {
const repl = REPL.start(app.config.info.name + ' > ' || 'strapi > '); // eslint-disable-line prefer-template const repl = REPL.start(app.config.info.name + ' > ' || 'strapi > '); // eslint-disable-line prefer-template

View File

@ -1,13 +1,20 @@
'use strict'; 'use strict';
const path = require('path');
const CLITable = require('cli-table3'); const CLITable = require('cli-table3');
const chalk = require('chalk'); const chalk = require('chalk');
const { toUpper } = require('lodash/fp'); const { toUpper } = require('lodash/fp');
const tsUtils = require('@strapi/typescript-utils');
const strapi = require('../../index'); const strapi = require('../../index');
module.exports = async function() { module.exports = async function() {
const app = await strapi().load(); const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir);
const distDir = isTSProject ? path.join(appDir, 'dist') : appDir;
const app = await strapi({ appDir, distDir }).load();
const list = app.server.listRoutes(); const list = app.server.listRoutes();

View File

@ -1,8 +1,14 @@
'use strict'; 'use strict';
const tsUtils = require('@strapi/typescript-utils')
const strapi = require('../index'); const strapi = require('../index');
/** /**
* `$ strapi start` * `$ strapi start`
*/ */
module.exports = distDir => strapi({ distDir }).start(); module.exports = async specifiedDir => {
const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir)
const distDir = isTSProject && !specifiedDir ? 'dist' : specifiedDir;
strapi({ distDir }).start()
};

View File

@ -15,6 +15,11 @@ const dbQuestions = require('./utils/db-questions');
const createProject = require('./create-project'); const createProject = require('./create-project');
module.exports = async scope => { module.exports = async scope => {
if (!scope.useTypescript) {
const language = await askAboutLanguages(scope);
scope.useTypescript = language === 'Typescript';
}
await trackUsage({ event: 'didChooseCustomDatabase', scope }); await trackUsage({ event: 'didChooseCustomDatabase', scope });
const configuration = await askDbInfosAndTest(scope).catch(error => { const configuration = await askDbInfosAndTest(scope).catch(error => {
@ -157,3 +162,17 @@ async function installDatabaseTestingDep({ scope, configuration }) {
await execa(packageManager, cmd.concat(deps)); await execa(packageManager, cmd.concat(deps));
} }
async function askAboutLanguages() {
const { language } = await inquirer.prompt([
{
type: 'list',
name: 'language',
message: 'Choose your preferred language',
choices: ['Javascript', 'Typescript'],
default: 'Javascript',
},
]);
return language;
}

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
const chalk = require('chalk'); const chalk = require('chalk');
const tsUtils = require('@strapi/typescript-utils');
const logInstructions = (pluginName, { language }) => { const logInstructions = (pluginName, { language }) => {
const maxLength = ` resolve: './src/plugins/${pluginName}'`.length; const maxLength = ` resolve: './src/plugins/${pluginName}'`.length;
@ -38,10 +37,17 @@ module.exports = plop => {
name: 'pluginName', name: 'pluginName',
message: 'Plugin name', message: 'Plugin name',
}, },
{
type: 'list',
name: 'language',
message: 'Choose your preferred language',
choices: ['Javascript', 'Typescript'],
default: 'Javascript',
},
], ],
actions(answers) { actions(answers) {
const currentDir = process.cwd(); const isTypescript = answers.language === 'Typescript';
const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js'; const language = isTypescript ? 'ts' : 'js';
// TODO: Adds tsconfig & build command for TS plugins? // TODO: Adds tsconfig & build command for TS plugins?