If your add-on adds dynamic options via addOption during setup, pass their
types as a type argument:
constaddon=defineAddon<{extra:boolean}>()({...});// Take note of the extra call here: 👆 👆// This works around Typescript's lack of partial type argumentsaddon.options.extra.default;// boolean
If your add-on adds dynamic options via addOption during setup, pass their
types as a type argument:
constaddon=defineAddon<{extra:boolean}>()({...});// Take note of the extra call here: 👆 👆// This works around Typescript's lack of partial type argumentsaddon.options.extra.default;// boolean
(), // appelé avant l'exécution — déclarer des dépendances, des prérequis d'environnement et des options dynamiques
setup(property)setup?:((workspace:Workspace&{dependsOn:(id:AddonId)=>void;unsupported:(reason:string)=>void;runsAfter:(id:AddonId)=>void;addOption:<Kextendsstring>(key:K,question:BaseQuestion<any>&Question<any>)=>void;})=>MaybePromise<void>)|undefined
Setup the addon. Will be called before the addon is run.
Required add-ons. Implies runsAfter. Official ids are installed automatically, others must be part of the same run.
('eslint'); // ajoute dynamiquement des options (par ex. basé sur l'état du workspace ou des données récupérées)
addOptionany('theme',{question(property)question:string:'Which theme?',type(property)type:string:'select',default(property)default:string:'dark',options(property)options:{value:string;}[]:[{value(property)value:string:'dark'},{value(property)value:string:'light'}]});}, // le travail effectif — ajouter, modifier des fichiers, déclarer des dépendances
run(property)run:(workspace:Workspace&{options:OptionValues<{}>&Record<string,unknown>;sv:SvApi;cancel:(reason:string)=>void;})=>MaybePromise<void>
Run the addon. The actual execution of the addon... Add files, edit files, etc.
Edit a file in the workspace. (will create it if it doesn't exist)
Return false from the callback to abort - the original content is returned unchanged.
('src/lib/foo.ts',(content(parameter)content:string)=>{return'export const foo = true;';}); // supprimer un fichier (respecte le filtre de fichier de migration)
sv(parameter)sv:SvApi
Edits matching files in the workspace.
The include and exclude patterns are glob patterns relative to the workspace root.
For each matching file, the edit callback is called with the file content,
and should return the new content (or false to abort editing that file).
Note: always adds excludes for node_modules and dot-prefixed directories
Returns true if searchString appears as a substring of the result of converting this
object to a String, at one or more positions that are
greater than or equal to position; otherwise, returns false.
searchString
search string
position
If position is undefined, 0 is assumed, so as to search all of the String.
(({ast(parameter)ast:AST.Root,svelte(parameter)svelte:typeofindex_d_exports$4})=>{svelte(parameter)svelte:typeofindex_d_exports$4.addFragment(alias)index_d_exports$4.addFragment(ast:AST.Root|AST.BaseElement,content:string,options?:{mode?:"append"|"prepend";language?:"ts"|"js";}):voidexportindex_d_exports$4.addFragment(ast(parameter)ast:AST.Root,'<p>Hello!</p>');})); // annuler à tout moment si quelque chose se passe mal
// cancel('reason');
}, // affiché après l'exécution de l'add-on
nextSteps(property)nextSteps?:((workspace:Workspace&{options:OptionValues<{}>&Record<string,unknown>;})=>string[])|undefined
Next steps to display after the addon is run.
:({options(parameter)options:OptionValues<{}>&Record<string,unknown>})=>['Lancer `npm run dev` pour commencer']});
L’objet sv dans run fournit file, files, removeFiles, dependency, devDependency, et
execute. Pour les transformations (l’édition de scripts, de composants Svelte, de CSS, de JSON,...
basée sur des AST), et les utilitaires de gestion de paquets, consultez
@sveltejs/sv-utils.
Options typées dynamiques
Si votre add-on ajoute des options dynamiquement dans setup, (par ex, depuis un appel fetch),
vous pouvez fournir un paramètre de type à defineAddon pour obtenir un typage fort pour ces
options :
If your add-on adds dynamic options via addOption during setup, pass their
types as a type argument:
constaddon=defineAddon<{extra:boolean}>()({...});// Take note of the extra call here: 👆 👆// This works around Typescript's lack of partial type argumentsaddon.options.extra.default;// boolean
Add-on options (includes dynamically added options from setup)
.theme(property)theme:string;// string}});
Le paramètre de type fait correspondre le type (boolean, string, number) aux définitions des
questions. Sans ce paramètre, defineAddon reste strict et autorise uniquement des options définies
de manière statique.
defineAddonOptions
Permet de construire des options pour les add-ons. Chaîné avec .add() et finalisé par .build().
}from'sv';constoptionsconstoptions:{database:{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];};docker:{readonlyquestion:"Ajouter un fichier docker-compose ?";readonlytype:"boolean";readonlydefault:false;readonlycondition:(opts:OptionValues<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{...;}];}>&Record<...>>)=>boolean;};}=defineAddonOptions(alias)defineAddonOptions<never>():OptionBuilder<{},never>importdefineAddonOptions
Options for an addon.
Will be prompted to the user if there are not answered by args when calling the cli.
().add(method)add<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}>(key:"database",question:{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}):OptionBuilder<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}>,never>
This type is a bit complex, but in usage, it's quite simple!
The idea is to add() options one by one, with the key and the question.
.add('demo',{question:'Do you want to add a demo?',type:'boolean',// string, number, select, multiselectdefault:true,// condition: (o) => o.previousOption === 'ok',})
('database',{question(property)question:"Quelle base de données ?":'Quelle base de données ?',type(property)type:"select":'select',default(property)default:"postgresql":'postgresql',options(property)options:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}]:[{value(property)value:"postgresql":'postgresql'},{value(property)value:"mysql":'mysql'},{value(property)value:"sqlite":'sqlite'}]}).add(method)add<"docker",{readonlyquestion:"Ajouter un fichier docker-compose ?";readonlytype:"boolean";readonlydefault:false;readonlycondition:(opts:OptionValues<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}>&Record<"docker",any>>)=>boolean;}>(key:"docker",question:{readonlyquestion:"Ajouter un fichier docker-compose ?";readonlytype:"boolean";readonlydefault:false;readonlycondition:(opts:OptionValues<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}>&Record<"docker",any>>)=>boolean;}):OptionBuilder<...>
This type is a bit complex, but in usage, it's quite simple!
The idea is to add() options one by one, with the key and the question.
.add('demo',{question:'Do you want to add a demo?',type:'boolean',// string, number, select, multiselectdefault:true,// condition: (o) => o.previousOption === 'ok',})
('docker',{question(property)question:"Ajouter un fichier docker-compose ?":'Ajouter un fichier docker-compose ?',type(property)type:"boolean":'boolean',default(property)default:false:false, // ne poser la question que lorsque la base de données n'est pas sqlite
condition(property)condition:(opts:OptionValues<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}>&Record<"docker",any>>)=>boolean:(opts(parameter)opts:OptionValues<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}>&Record<"docker",any>>)=>opts(parameter)opts:OptionValues<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];}>&Record<"docker",any>>.database(property)database:"postgresql"|"mysql"|"sqlite"!=='sqlite'}).build(method)build():{database:{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{readonlyvalue:"sqlite";}];};docker:{readonlyquestion:"Ajouter un fichier docker-compose ?";readonlytype:"boolean";readonlydefault:false;readonlycondition:(opts:OptionValues<Record<"database",{readonlyquestion:"Quelle base de données ?";readonlytype:"select";readonlydefault:"postgresql";readonlyoptions:[{readonlyvalue:"postgresql";},{readonlyvalue:"mysql";},{...;}];}>&Record<...>>)=>boolean;};}
Finalize all options of your add-on.
();
Les options sont posées dans l’ordre. Le callback condition reçoit les réponses collectées jusque
là — renvoyez false pour éviter la question (sa valeur sera undefined).