2020-10-27 11:27:17 +01:00
'use strict' ;
2021-09-08 17:25:24 +02:00
2019-04-29 15:48:16 +02:00
const path = require ( 'path' ) ;
2023-10-30 11:36:16 +00:00
const fs = require ( 'fs/promises' ) ;
const os = require ( 'os' ) ;
2019-09-24 14:10:57 +02:00
const chalk = require ( 'chalk' ) ;
2023-10-30 11:36:16 +00:00
const ora = require ( 'ora' ) ;
const ts = require ( 'typescript' ) ;
const strapi = require ( '@strapi/strapi' ) ;
const { build : nodeBuild , develop : nodeDevelop } = require ( './dist/cli' ) ;
/ * *
* @ typedef { Object } BuildArgs
* @ property { boolean } optimize
* /
/ * *
* @ deprecated From V5 we will not be exporting build functions from the root export of the admin package .
*
* @ type { ( args : BuildArgs ) => Promise < void > }
* /
async function build ( { optimize } ) {
console . warn (
"[@strapi/admin]: the build api exported from this package is now deprecated. We don't plan to expose this for public consumption and this will be removed in V5."
) ;
2022-04-14 10:59:42 +02:00
2023-10-30 11:36:16 +00:00
const enforceSourceMaps = process . env . STRAPI _ENFORCE _SOURCEMAPS === 'true' ? ? false ;
2020-04-15 12:56:51 +02:00
2023-10-30 11:36:16 +00:00
const cwd = process . cwd ( ) ;
const logger = createLogger ( { debug : true , silent : false , timestamp : false } ) ;
2022-04-14 10:59:42 +02:00
2023-10-30 11:36:16 +00:00
const tsconfig = loadTsConfig ( {
cwd ,
path : 'tsconfig.json' ,
logger ,
} ) ;
2021-12-20 14:56:06 +01:00
2023-10-30 11:36:16 +00:00
const distDir = tsconfig ? . config . options . outDir ? ? '' ;
const strapiInstance = strapi ( {
// Directories
appDir : cwd ,
distDir ,
// Options
autoReload : true ,
serveAdminPanel : false ,
} ) ;
await nodeBuild ( {
cwd ,
logger ,
minify : optimize ,
sourcemaps : enforceSourceMaps ,
strapi : strapiInstance ,
tsconfig ,
} ) ;
}
2021-11-29 16:13:30 +01:00
2023-10-30 11:36:16 +00:00
/ * *
* @ typedef { Object } CleanArgs
* @ property { string } appDir
* @ property { string } buildDestDir
* /
/ * *
* @ deprecated From V5 we will not be exporting clean functions from the root export of the admin package .
*
* @ type { ( args : CleanArgs ) => Promise < void > }
* /
async function clean ( { appDir , buildDestDir } ) {
console . warn (
"[@strapi/admin]: the clean api exported from this package is now deprecated. We don't plan to expose this for public consumption and this will be removed in V5."
) ;
2020-04-15 11:56:30 +02:00
2023-10-30 11:36:16 +00:00
const DIRECTORIES = [
path . join ( buildDestDir , 'build' ) ,
path . join ( appDir , '.cache' ) ,
path . join ( appDir , '.strapi' ) ,
] ;
2021-07-01 14:19:50 +02:00
2023-10-30 11:36:16 +00:00
await Promise . all ( DIRECTORIES . map ( ( dir ) => fs . rmdir ( dir , { recursive : true , force : true } ) ) ) ;
}
2021-09-09 10:56:57 +02:00
2023-10-30 11:36:16 +00:00
/ * *
* @ typedef { Object } WatchArgs
* @ property { boolean } browser
* @ property { boolean } open
* @ property { boolean } polling
* /
/ * *
* @ deprecated From V5 we will not be exporting watch functions from the root export of the admin package .
*
* @ type { ( args : WatchArgs ) => Promise < void > }
* /
async function watchAdmin ( { browser , open , polling } ) {
console . warn (
[
"[@strapi/admin]: the watchAdmin api exported from this package is now deprecated. We don't plan to expose this for public consumption and this will be removed in V5." ,
"This command is no longer necessary, the admin's dev server is now injected as a middleware to the strapi server. This is why we're about to start a strapi instance for you." ,
] . join ( os . EOL )
) ;
const cwd = process . cwd ( ) ;
const logger = createLogger ( { debug : true , silent : false , timestamp : false } ) ;
const tsconfig = loadTsConfig ( {
cwd ,
path : 'tsconfig.json' ,
logger ,
2021-09-09 10:56:57 +02:00
} ) ;
2020-04-15 11:56:30 +02:00
2023-10-30 11:36:16 +00:00
const distDir = tsconfig ? . config . options . outDir ? ? '' ;
2020-04-15 11:56:30 +02:00
2023-10-30 11:36:16 +00:00
const strapiInstance = strapi ( {
// Directories
appDir : cwd ,
distDir ,
// Options
autoReload : true ,
serveAdminPanel : false ,
} ) ;
2021-07-01 14:19:50 +02:00
2023-10-30 11:36:16 +00:00
await nodeDevelop ( {
cwd ,
logger ,
browser ,
open ,
polling ,
strapi : strapiInstance ,
tsconfig ,
} ) ;
}
/ * *
* @ internal
* /
const createLogger = ( options = { } ) => {
const { silent = false , debug = false , timestamp = true } = options ;
const state = { errors : 0 , warning : 0 } ;
return {
get warnings ( ) {
return state . warning ;
} ,
get errors ( ) {
return state . errors ;
} ,
debug ( ... args ) {
if ( silent || ! debug ) {
return ;
2020-04-15 11:56:30 +02:00
}
2023-10-30 11:36:16 +00:00
console . log (
chalk . cyan ( ` [DEBUG] ${ timestamp ? ` \t [ ${ new Date ( ) . toISOString ( ) } ] ` : '' } ` ) ,
... args
) ;
} ,
2021-07-01 14:19:50 +02:00
2023-10-30 11:36:16 +00:00
info ( ... args ) {
if ( silent ) {
return ;
2020-04-15 11:56:30 +02:00
}
2023-10-30 11:36:16 +00:00
console . info (
chalk . blue ( ` [INFO] ${ timestamp ? ` \t [ ${ new Date ( ) . toISOString ( ) } ] ` : '' } ` ) ,
... args
) ;
} ,
2021-07-01 14:19:50 +02:00
2023-10-30 11:36:16 +00:00
log ( ... args ) {
if ( silent ) {
return ;
}
2020-04-15 11:56:30 +02:00
2023-10-30 11:36:16 +00:00
console . info ( chalk . blue ( ` ${ timestamp ? ` \t [ ${ new Date ( ) . toISOString ( ) } ] ` : '' } ` ) , ... args ) ;
} ,
2020-04-14 15:24:14 +02:00
2023-10-30 11:36:16 +00:00
warn ( ... args ) {
state . warning += 1 ;
2020-04-14 15:24:14 +02:00
2023-10-30 11:36:16 +00:00
if ( silent ) {
return ;
}
console . warn (
chalk . yellow ( ` [WARN] ${ timestamp ? ` \t [ ${ new Date ( ) . toISOString ( ) } ] ` : '' } ` ) ,
... args
) ;
2019-09-23 19:27:40 +02:00
} ,
2023-10-30 11:36:16 +00:00
error ( ... args ) {
state . errors += 1 ;
if ( silent ) {
return ;
}
console . error (
chalk . red ( ` [ERROR] ${ timestamp ? ` \t [ ${ new Date ( ) . toISOString ( ) } ] ` : '' } ` ) ,
... args
) ;
} ,
2022-01-17 12:05:45 +01:00
2023-10-30 11:36:16 +00:00
spinner ( text ) {
if ( silent ) {
return {
succeed ( ) {
return this ;
} ,
fail ( ) {
return this ;
} ,
start ( ) {
return this ;
} ,
text : '' ,
} ;
}
2019-09-23 19:27:40 +02:00
2023-10-30 11:36:16 +00:00
return ora ( text ) ;
} ,
2022-03-02 10:08:55 +01:00
} ;
2023-10-30 11:36:16 +00:00
} ;
2022-02-25 23:20:08 +01:00
2023-10-30 11:36:16 +00:00
/ * *
* @ internal
* /
const loadTsConfig = ( { cwd , path , logger } ) => {
const configPath = ts . findConfigFile ( cwd , ts . sys . fileExists , path ) ;
2019-09-24 07:10:02 +02:00
2023-10-30 11:36:16 +00:00
if ( ! configPath ) {
return undefined ;
}
2022-01-17 12:05:45 +01:00
2023-10-30 11:36:16 +00:00
const configFile = ts . readConfigFile ( configPath , ts . sys . readFile ) ;
2022-01-17 12:05:45 +01:00
2023-10-30 11:36:16 +00:00
const parsedConfig = ts . parseJsonConfigFileContent ( configFile . config , ts . sys , cwd ) ;
2021-07-27 09:26:37 +02:00
2023-10-30 11:36:16 +00:00
logger . debug ( ` Loaded user TS config: ` , os . EOL , parsedConfig ) ;
return {
config : parsedConfig ,
path : configPath ,
} ;
} ;
2021-11-29 16:13:30 +01:00
2019-04-29 15:48:16 +02:00
module . exports = {
2020-04-14 15:24:14 +02:00
clean ,
2019-04-29 15:48:16 +02:00
build ,
2019-09-26 11:52:02 +02:00
watchAdmin ,
2019-04-29 15:48:16 +02:00
} ;