creatium 0.0.5 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.d.mts CHANGED
@@ -9,6 +9,7 @@ import { globby, globbyStream } from 'globby';
9
9
  import { writeFile as writeFile$1, readFile as readFile$1 } from 'node:fs/promises';
10
10
  import { fileURLToPath } from 'node:url';
11
11
  import { Options as Options$2 } from 'yargs';
12
+ import { UpdateNotifier } from 'update-notifier';
12
13
 
13
14
  /**
14
15
  * Any type
@@ -719,7 +720,12 @@ type CoreInterface<V = string> = {
719
720
  *
720
721
  * Used to validate initial value in prompt method
721
722
  */
722
- validateInitialValue: () => Promise<V | undefined>;
723
+ validateInitialValue: (data: {
724
+ /** Show success message */
725
+ showSuccess?: boolean;
726
+ /** Show error message */
727
+ showError?: boolean;
728
+ }) => Promise<V | undefined>;
723
729
  /**
724
730
  * Method to set the cli option
725
731
  * @returns {Promise<CliOption | undefined>}
@@ -769,7 +775,10 @@ type OptionSelect<V extends Value$5 = Value$5> = OptionCommonWithPlaceholder<V>
769
775
  };
770
776
  declare class Select<V extends Value$5 = Value$5> extends Core<OptionSelect<V>, V> implements CoreInterface<Value$5> {
771
777
  cmd(): Promise<CliOption>;
772
- validateInitialValue(): Promise<V | undefined>;
778
+ validateInitialValue(data?: {
779
+ showSuccess?: boolean;
780
+ showError?: boolean;
781
+ }): Promise<V | undefined>;
773
782
  prompt(): Promise<string>;
774
783
  }
775
784
 
@@ -797,20 +806,25 @@ type SelectBaseOptions<Value extends string = string> = Partial<Omit<OptionSelec
797
806
  };
798
807
  };
799
808
 
800
- declare const IDE: {
809
+ /** Text editor values used in `openEditor` option. */
810
+ declare const TEXT_EDITOR: {
801
811
  readonly VSCODE: "code";
802
812
  readonly SUBLIME: "subl";
803
813
  readonly WEBSTORM: "webstorm";
804
814
  readonly NONE: "none";
805
815
  };
806
- type TextEditor = ObjectValues<typeof IDE>;
816
+ type TextEditor = ObjectValues<typeof TEXT_EDITOR>;
807
817
  type OptionEditor = SelectBaseOptions<TextEditor>;
808
818
  declare class Editor extends Select<TextEditor> {
809
819
  constructor(config: OptionEditor);
810
- validateInitialValue(): Promise<TextEditor | undefined>;
820
+ validateInitialValue(data?: {
821
+ showSuccess?: boolean;
822
+ showError?: boolean;
823
+ }): Promise<TextEditor | undefined>;
811
824
  prompt(): Promise<TextEditor>;
812
825
  }
813
826
 
827
+ /** installer values used in `install` option. */
814
828
  declare const INSTALLER: {
815
829
  readonly NONE: "none";
816
830
  readonly DENO: "deno";
@@ -819,12 +833,15 @@ declare const INSTALLER: {
819
833
  readonly PNPM: "pnpm";
820
834
  readonly YARN: "yarn";
821
835
  };
822
- type PkgManager = ObjectValues<typeof INSTALLER>;
823
- type OptionInstall = SelectBaseOptions<PkgManager>;
824
- declare class Install extends Select<PkgManager> {
836
+ type Installer = ObjectValues<typeof INSTALLER>;
837
+ type OptionInstall = SelectBaseOptions<Installer>;
838
+ declare class Install extends Select<Installer> {
825
839
  constructor(config: OptionInstall);
826
- validateInitialValue(): Promise<PkgManager | undefined>;
827
- prompt(): Promise<PkgManager>;
840
+ validateInitialValue(data?: {
841
+ showSuccess?: boolean;
842
+ showError?: boolean;
843
+ }): Promise<Installer | undefined>;
844
+ prompt(): Promise<Installer>;
828
845
  }
829
846
 
830
847
  /** Object of the CREATIUM types */
@@ -855,7 +872,10 @@ type OptionArray = OptionCommonWithPlaceholder<Value$4> & {
855
872
  declare class Array extends Core<OptionArray, Value$4> implements CoreInterface<Value$4> {
856
873
  #private;
857
874
  cmd(): Promise<CliOption>;
858
- validateInitialValue(): Promise<Value$4 | undefined>;
875
+ validateInitialValue(data?: {
876
+ showSuccess?: boolean;
877
+ showError?: boolean;
878
+ }): Promise<Value$4 | undefined>;
859
879
  prompt(): Promise<string[]>;
860
880
  }
861
881
 
@@ -863,7 +883,10 @@ type Value$3 = boolean;
863
883
  type OptionBoolean = OptionCommonWithPlaceholder<Value$3>;
864
884
  declare class Boolean extends Core<OptionBoolean, Value$3> implements CoreInterface<Value$3> {
865
885
  cmd(): Promise<CliOption>;
866
- validateInitialValue(): Promise<boolean | undefined>;
886
+ validateInitialValue(data?: {
887
+ showSuccess?: boolean;
888
+ showError?: boolean;
889
+ }): Promise<boolean | undefined>;
867
890
  prompt(): Promise<boolean>;
868
891
  }
869
892
 
@@ -881,7 +904,10 @@ type OptionMultiselect<V extends Value$2 = Value$2> = OptionCommonWithPlaceholde
881
904
  };
882
905
  declare class Multiselect<V extends Value$2 = Value$2> extends Core<OptionMultiselect<V>, V[]> implements CoreInterface<V[]> {
883
906
  cmd(): Promise<CliOption>;
884
- validateInitialValue(): Promise<V[] | undefined>;
907
+ validateInitialValue(data?: {
908
+ showSuccess?: boolean;
909
+ showError?: boolean;
910
+ }): Promise<V[] | undefined>;
885
911
  prompt(): Promise<string[]>;
886
912
  }
887
913
 
@@ -889,7 +915,10 @@ type Value$1 = number;
889
915
  type OptionNumber = OptionCommonWithPlaceholder<Value$1>;
890
916
  declare class Number extends Core<OptionNumber, Value$1> implements CoreInterface<Value$1> {
891
917
  cmd(): Promise<CliOption>;
892
- validateInitialValue(): Promise<number | undefined>;
918
+ validateInitialValue(data?: {
919
+ showSuccess?: boolean;
920
+ showError?: boolean;
921
+ }): Promise<number | undefined>;
893
922
  prompt(): Promise<number>;
894
923
  }
895
924
 
@@ -897,7 +926,10 @@ type Value = string;
897
926
  type OptionText = OptionCommonWithPlaceholder<Value>;
898
927
  declare class Text extends Core<OptionText, Value> implements CoreInterface<Value> {
899
928
  cmd(): Promise<CliOption>;
900
- validateInitialValue(): Promise<string | undefined>;
929
+ validateInitialValue(data?: {
930
+ showSuccess?: boolean;
931
+ showError?: boolean;
932
+ }): Promise<string | undefined>;
901
933
  prompt(): Promise<string>;
902
934
  }
903
935
 
@@ -913,7 +945,10 @@ declare class Void extends Core<OptionVoid, void> implements CoreInterface<void>
913
945
  type OptionName = Partial<Name['config']>;
914
946
  declare class Name extends Text {
915
947
  constructor(config: OptionName);
916
- validateInitialValue(): Promise<string | undefined>;
948
+ validateInitialValue(data?: {
949
+ showSuccess?: boolean;
950
+ showError?: boolean;
951
+ }): Promise<string | undefined>;
917
952
  prompt(): Promise<string>;
918
953
  }
919
954
 
@@ -937,7 +972,10 @@ type OptionPath = Partial<Path['config']> & {
937
972
  declare class Path extends Text {
938
973
  #private;
939
974
  constructor(config: OptionPath);
940
- validateInitialValue(): Promise<string | undefined>;
975
+ validateInitialValue(data?: {
976
+ showSuccess?: boolean;
977
+ showError?: boolean;
978
+ }): Promise<string | undefined>;
941
979
  prompt(): Promise<string>;
942
980
  }
943
981
 
@@ -1044,7 +1082,7 @@ type CreateTemplateOpts = {
1044
1082
  /** Set the name of the template */
1045
1083
  name?: string;
1046
1084
  /** Set the installer */
1047
- install?: PkgManager;
1085
+ install?: Installer;
1048
1086
  /**
1049
1087
  * Open editor
1050
1088
  */
@@ -1137,6 +1175,69 @@ type CreateOpts = CliOpts & {
1137
1175
  activeCli?: boolean;
1138
1176
  };
1139
1177
 
1178
+ type ParamFn = (arg: string) => Promise<string>;
1179
+ type ParamsValue = string | number | Record<string, unknown> | unknown[] | unknown;
1180
+ type Params = Record<string, ParamsValue>;
1181
+ type Props = {
1182
+ /** Content to be replaced */
1183
+ content: string;
1184
+ /**
1185
+ * Parameters
1186
+ * @example
1187
+ * const params = {
1188
+ * name: 'Antonio',
1189
+ * lastName : 'Resines'
1190
+ * }
1191
+ */
1192
+ params: Params;
1193
+ /**
1194
+ * Transform parameters insde placeholders.
1195
+ * @example
1196
+ * const transform = async ( param: string ) => {
1197
+ * if ( param === 'url' ) return 'https://pigeonposse.com',
1198
+ * else if ( param === 'http://pigeonposse.com' ) return 'https://pigeonposse.com'
1199
+ * return param
1200
+ * }
1201
+ */
1202
+ transform?: ParamFn;
1203
+ /** Options */
1204
+ opts?: {
1205
+ /**
1206
+ * Throw an error if a placeholder is not found.
1207
+ * @default false
1208
+ */
1209
+ throw?: boolean;
1210
+ /**
1211
+ * Throw an error if a parameter is not found.
1212
+ * @default
1213
+ * {
1214
+ * prefix : '{{',
1215
+ * suffix : '}}',
1216
+ * }
1217
+ */
1218
+ mark?: {
1219
+ prefix: string;
1220
+ suffix: string;
1221
+ };
1222
+ };
1223
+ };
1224
+ /**
1225
+ * Replace placeholders in a string with their corresponding values.
1226
+ *
1227
+ * The function takes a string with placeholders, an object with parameter values,
1228
+ * and an optional custom parameter function.
1229
+ *
1230
+ * The function returns a Promise that resolves to the string with all placeholders
1231
+ * replaced.
1232
+ * @param {Props} props - Props for the function.
1233
+ * @param {Props['content']} props.content - The string with placeholders.
1234
+ * @param {Props['params']} props.params - An object with parameter values.
1235
+ * @param {Props['transform']} [props.transform] - An optional custom parameter function.
1236
+ * @param {Props['opts']} [props.opts] - Options to customize the behavior of the function.
1237
+ * @returns {Promise<string>} - A Promise that resolves to the string with all placeholders replaced.
1238
+ */
1239
+ declare const replacePlaceholders: (props: Props) => Promise<string>;
1240
+
1140
1241
  type GetPromptValues<C extends Config> = Prettify<{
1141
1242
  [K in keyof C['prompt']]?: Prettify<(C['prompt'][K] extends {
1142
1243
  type: infer T;
@@ -1147,9 +1248,9 @@ type GetPromptValues<C extends Config> = Prettify<{
1147
1248
  * Customizable class of `Creatium` for create project templates (CLI and Library).
1148
1249
  * @template C
1149
1250
  * @example
1150
- * //////////////// main.js ///////////////////
1251
+ * //////////////// core.js ///////////////////
1151
1252
  *
1152
- * const core = new CreatiumPrompt({
1253
+ * export const core = new CreatiumCore({
1153
1254
  * name: 'My Project',
1154
1255
  * version: '1.0.0',
1155
1256
  * prompts: {
@@ -1160,27 +1261,162 @@ type GetPromptValues<C extends Config> = Prettify<{
1160
1261
  *
1161
1262
  * //////////////// bin.js ///////////////////
1162
1263
  *
1163
- * import { core } from './main.js'
1264
+ * import { core } from './core.js'
1164
1265
  * const res = await core.cli()
1165
1266
  * // do something with res...
1166
1267
  * await core.createTemplate( res )
1167
1268
  *
1168
1269
  * //////////////// lib.js ///////////////////
1169
1270
  *
1170
- * import { core } from './main.js'
1271
+ * import { core } from './core.js'
1171
1272
  * export const create = async (args) => {
1172
1273
  * const res = await core.build( args )
1173
1274
  * // do something with res...
1174
1275
  * await core.createTemplate( res )
1175
1276
  * }
1176
1277
  */
1177
- declare class CreatiumPrompt<C extends Config = Config> {
1278
+ declare class CreatiumCore<C extends Config = Config> {
1178
1279
  #private;
1179
1280
  utils: typeof ___shared_utils;
1180
1281
  config: C;
1181
1282
  constructor(config: C);
1182
1283
  /** Force debug mode */
1183
1284
  set debugMode(value: boolean);
1285
+ /**
1286
+ * Shows a notification if the current package is outdated.
1287
+ *
1288
+ * **information**: If this 'custom' function is provided, the default
1289
+ * notification will not be shown.
1290
+ *
1291
+ * ---
1292
+ * @param {object} [opts] - Options for the update notification.
1293
+ * @param {object} [opts.opts] - Options for the `update-notifier` package.
1294
+ * @param {Function} [opts.custom] - A custom function to run with the update
1295
+ * @example
1296
+ * // With default options. Recommended for most use cases.
1297
+ * await core.updateNotify()
1298
+ * @example
1299
+ * // With custom options
1300
+ * await core.updateNotify({ opts: { ... } })
1301
+ * @example
1302
+ * // With custom function
1303
+ * await core.updateNotify({ custom: () => {} })
1304
+ */
1305
+ updateNotify({ custom, opts, }?: {
1306
+ opts?: Parameters<UpdateNotifier['notify']>[0];
1307
+ custom?: (info?: UpdateNotifier['update']) => Response<void>;
1308
+ }): Promise<void>;
1309
+ /**
1310
+ * Cancels the current process and exits with code 0.
1311
+ *
1312
+ * If a `message` is provided, it will be displayed in the console.
1313
+ * If `onCancel` is set in the config, it will be called with the current data.
1314
+ * If `onCancel` is not set, a default message will be displayed.
1315
+ * @param {string} [message] - The message to display before exiting.
1316
+ */
1317
+ cancel(message?: string): Promise<void>;
1318
+ /**
1319
+ * Intro prompt line.
1320
+ *
1321
+ * If the parameter `message` is provided, it will be used as the intro message.
1322
+ * If the `intro` option is a function, it will be called with the `this.#data` as the argument.
1323
+ * If the `intro` option is undefined, the default intro message will be used.
1324
+ * @param {string} [message] The intro message.
1325
+ */
1326
+ intro(message?: string): Promise<void>;
1327
+ /**
1328
+ * Outro prompt line.
1329
+ *
1330
+ * If the parameter `message` is provided, it will be used as the outro message.
1331
+ * If the `outro` option is a function, it will be called with the `this.#data` as the argument.
1332
+ * If the `outro` option is undefined, the default outro message will be used.
1333
+ * @param {string} [message] The outro message.
1334
+ */
1335
+ outro(message?: string): Promise<void>;
1336
+ /**
1337
+ * Copy a directory from input path to output path.
1338
+ * @param {object} data - Options object with input and output paths.
1339
+ * @param {string} data.input - The path to the directory to copy.
1340
+ * @param {string} data.output - The path to the destination directory.
1341
+ * @returns {Promise<void>} - Resolves when the directory has been copied.
1342
+ * @example
1343
+ *
1344
+ * const copyResult = await core.copyDir({
1345
+ * input : '/path/to/sourceDir',
1346
+ * output: '/path/to/destinationDir',
1347
+ * })
1348
+ */
1349
+ copyDir(data: {
1350
+ input: string;
1351
+ output: string;
1352
+ }): Promise<void>;
1353
+ /**
1354
+ * Installs the project with the given package manager.
1355
+ * @param {object} [options] - The options to install.
1356
+ * @param {Installer} [options.installer] - The package manager to use for the installation.
1357
+ * @param {string} [options.input] - The path to the folder. If not provided, the current directory is used.
1358
+ * @returns {Promise<void>}
1359
+ * @example
1360
+ * await core.install( {
1361
+ * installer : 'pnpm',
1362
+ * input : 'my/project/path',
1363
+ * } )
1364
+ */
1365
+ install({ installer, input, }?: {
1366
+ installer?: Installer;
1367
+ input?: string;
1368
+ }): Promise<void>;
1369
+ /**
1370
+ * Open the project in the given editor.
1371
+ * @param {object} params - The parameters for opening the editor.
1372
+ * @param {TextEditor} params.editor - The editor to open the project in.
1373
+ * @param {string} params.input - The input path. If not provided, the current directory is used.
1374
+ * @returns {Promise<void>}
1375
+ * @example
1376
+ * await core.openEditor( {
1377
+ * editor : 'vscode',
1378
+ * input : 'my/project/path',
1379
+ * })
1380
+ */
1381
+ openEditor({ editor, input, }?: {
1382
+ editor?: TextEditor;
1383
+ input?: string;
1384
+ }): Promise<void>;
1385
+ /**
1386
+ * Replaces placeholders in files within the specified directory.
1387
+ *
1388
+ * This function searches for files in the provided input directory and replaces
1389
+ * placeholders within those files using the specified parameters. The placeholders
1390
+ * in the content are replaced with values from the `params` object.
1391
+ * @param {object} args - The arguments object.
1392
+ * @param {string} [args.input] - The directory path containing files with placeholders.
1393
+ * @param {object} [args.params] - An object containing key-value pairs for replacing placeholders.
1394
+ * @returns {Promise<void>} A Promise that resolves once all placeholders have been replaced.
1395
+ * @example
1396
+ * await core.replacePlaceholders( {
1397
+ * input : 'my/project/path',
1398
+ * params : { placeholder1: 'value1', placeholder2: 'value2' },
1399
+ * })
1400
+ */
1401
+ replacePlaceholders({ input, params, }?: {
1402
+ input?: string;
1403
+ params?: Parameters<typeof replacePlaceholders>[0]['params'];
1404
+ }): Promise<void>;
1405
+ /**
1406
+ * Return the input path of a template by name or path.
1407
+ * @param {string} [input] The name of the template or the path to the template.
1408
+ * @returns {Promise<string | undefined>} The input path of the template or undefined if not found.
1409
+ * @example
1410
+ * // with template path
1411
+ * const input = await core.getTemplateInput( { input : 'my/template/path' } )
1412
+ * @example
1413
+ * // With template key
1414
+ * // template key must be specified in the config prompt secction.
1415
+ * const input = await core.getTemplateInput( { input : 'templateKey' )
1416
+ */
1417
+ getTemplateInput({ input }?: {
1418
+ input?: string;
1419
+ }): Promise<string | undefined>;
1184
1420
  /**
1185
1421
  * Create a new project template.
1186
1422
  * @param {CreateTemplateOpts} values - The values to create the template.
@@ -1242,22 +1478,25 @@ declare class CreatiumPrompt<C extends Config = Config> {
1242
1478
  /**
1243
1479
  * Class of `Creatium` for create project templates (CLI and Library).
1244
1480
  * @example
1245
- * // ./main.js
1246
- * export const core = new CreatiumPrompt({
1481
+ * //////////////// core.js ///////////////////
1482
+ *
1483
+ * import { Creatium } from 'creatium'
1484
+ * export const core = new Creatium({
1247
1485
  * name: 'My Project',
1248
1486
  * version: '1.0.0',
1249
- * prompts: {
1487
+ * templates: {
1250
1488
  * ...
1251
1489
  * },
1252
- * ...
1253
1490
  * })
1254
1491
  *
1255
- * // ./bin.js
1256
- * import { core } from './main.js'
1492
+ * //////////////// bin.js ///////////////////
1493
+ *
1494
+ * import { core } from './core.js'
1257
1495
  * core.cli()
1258
1496
  *
1259
- * // ./lib.js
1260
- * import { core } from './main.js'
1497
+ * //////////////// lib.js ///////////////////
1498
+ *
1499
+ * import { core } from './core.js'
1261
1500
  * export const create = core.build
1262
1501
  */
1263
1502
  declare class Creatium {
@@ -1339,4 +1578,4 @@ declare class Creatium {
1339
1578
  }>;
1340
1579
  }
1341
1580
 
1342
- export { type CliOpts, type Config, type CreateOpts, type CreateTemplateOpts, Creatium, CreatiumPrompt, IDE, INSTALLER, OPTION, env, prompt, style, sys };
1581
+ export { type CliOpts, type Config, type CreateOpts, type CreateTemplateOpts, Creatium, CreatiumCore, INSTALLER, OPTION, TEXT_EDITOR, env, prompt, style, sys };