0
# CLI Commands
1
2
Sails.js provides a comprehensive command-line interface with 16+ commands for application development, testing, and deployment. The CLI is built on Commander.js and offers extensive options and aliases for efficient development workflows.
3
4
## Core Commands
5
6
### new
7
8
Create a new Sails application:
9
10
```bash { .api }
11
sails new [path] [options]
12
```
13
14
**Parameters**:
15
- `path` (String, optional) - Directory name for new application (default: current directory)
16
17
**Options**:
18
- `--no-frontend` - Skip frontend dependencies and assets
19
- `--fast` - Skip npm install (install dependencies manually later)
20
21
**Examples**:
22
```bash
23
# Create new app in current directory
24
sails new
25
26
# Create new app in specific directory
27
sails new my-awesome-app
28
29
# Create API-only app without frontend
30
sails new my-api --no-frontend
31
32
# Create app without installing dependencies
33
sails new my-app --fast
34
cd my-app
35
npm install
36
```
37
38
**Generated Structure**:
39
```
40
my-app/
41
├── api/
42
│ ├── controllers/
43
│ ├── models/
44
│ ├── policies/
45
│ └── services/
46
├── config/
47
├── views/
48
├── assets/
49
├── package.json
50
└── app.js
51
```
52
53
### lift
54
55
Start the Sails application:
56
57
```bash { .api }
58
sails lift [options]
59
sails l [options] # Alias
60
```
61
62
**Options**:
63
- `--prod` - Start in production environment
64
- `--staging` - Start in staging environment
65
- `--port [port]` - Specify port number
66
- `--silent` - Suppress all log output
67
- `--verbose` - Enable verbose logging
68
- `--silly` - Enable maximum logging detail
69
70
**Examples**:
71
```bash
72
# Basic lift
73
sails lift
74
75
# Production mode
76
sails lift --prod
77
78
# Custom port
79
sails lift --port 3000
80
81
# Verbose logging
82
sails lift --verbose
83
84
# Multiple options
85
sails lift --prod --port 8080 --silent
86
```
87
88
**Environment Variables**:
89
```bash
90
# Set environment via variable
91
NODE_ENV=production sails lift
92
93
# Set port via variable
94
PORT=3000 sails lift
95
```
96
97
### generate
98
99
Generate code components (controllers, models, actions, etc.):
100
101
```bash { .api }
102
sails generate [type] [name] [options]
103
```
104
105
**Available Generators**:
106
- `controller` - Generate controller
107
- `model` - Generate model
108
- `action` - Generate standalone action
109
- `helper` - Generate helper function
110
- `page` - Generate page (controller + view)
111
- `api` - Generate model + controller
112
113
**Examples**:
114
```bash
115
# Generate controller
116
sails generate controller User
117
118
# Generate model
119
sails generate model Product name:string price:number
120
121
# Generate API (model + controller)
122
sails generate api Pet name:string breed:string
123
124
# Generate standalone action
125
sails generate action auth/login
126
127
# Generate helper
128
sails generate helper format-date
129
130
# Generate page with view
131
sails generate page dashboard
132
```
133
134
**Model Attributes**:
135
```bash
136
# Generate model with attributes and types
137
sails generate model User \
138
email:string \
139
firstName:string \
140
lastName:string \
141
age:number \
142
isActive:boolean \
143
avatar:string
144
```
145
146
## Development Commands
147
148
### console
149
150
Start an interactive REPL with application context:
151
152
```bash { .api }
153
sails console [options]
154
sails c [options] # Alias
155
```
156
157
**Options**:
158
- `--dontLift` - Don't lift the Sails app (access limited functionality)
159
- `--silent` - Suppress lift messages
160
- `--verbose` - Enable verbose logging
161
- `--silly` - Enable maximum logging
162
163
**Examples**:
164
```bash
165
# Start console with full app context
166
sails console
167
168
# Start console without lifting app
169
sails console --dontLift
170
171
# Start console with verbose output
172
sails console --verbose
173
```
174
175
**Console Usage**:
176
```javascript
177
// Access models
178
sails> User.find()
179
sails> await User.create({email: 'test@example.com', name: 'Test User'})
180
181
// Access services
182
sails> EmailService.sendWelcomeEmail('user@example.com')
183
184
// Access configuration
185
sails> sails.config.datastores
186
sails> sails.config.environment
187
188
// Run helpers
189
sails> await sails.helpers.formatDate(new Date())
190
191
// Make virtual requests
192
sails> sails.request('/api/users')
193
```
194
195
### debug
196
197
Start Sails with Node.js debugger (Node v5 and below):
198
199
```bash { .api }
200
sails debug [options]
201
```
202
203
**Options**:
204
- Same options as `sails lift`
205
206
**Example**:
207
```bash
208
# Start with debugger
209
sails debug
210
211
# Debug in production mode
212
sails debug --prod
213
```
214
215
### inspect
216
217
Start Sails with Node.js inspector (Node v6 and above):
218
219
```bash { .api }
220
sails inspect [options]
221
```
222
223
**Options**:
224
- Same options as `sails lift`
225
226
**Examples**:
227
```bash
228
# Start with inspector
229
sails inspect
230
231
# Inspect with custom port
232
sails inspect --port 3000
233
234
# Open Chrome DevTools at chrome://inspect
235
```
236
237
**Inspector Usage**:
238
1. Run `sails inspect`
239
2. Open Chrome browser
240
3. Navigate to `chrome://inspect`
241
4. Click "inspect" under Remote Target
242
243
## Utility Commands
244
245
### run
246
247
Run custom scripts defined in your application:
248
249
```bash { .api }
250
sails run [script] [options]
251
```
252
253
**Parameters**:
254
- `script` (String) - Script name from `scripts/` directory
255
256
**Examples**:
257
```bash
258
# Run custom script
259
sails run bootstrap-data
260
261
# Run with arguments
262
sails run import-users --file=users.csv
263
264
# Run migration script
265
sails run migrate-database
266
```
267
268
**Script Structure** (`scripts/bootstrap-data.js`):
269
```javascript
270
module.exports = {
271
friendlyName: 'Bootstrap data',
272
description: 'Set up initial application data',
273
274
fn: async function(inputs) {
275
console.log('Bootstrapping initial data...');
276
277
// Create admin user
278
const admin = await User.create({
279
email: 'admin@example.com',
280
role: 'admin'
281
}).fetch();
282
283
console.log('Admin user created:', admin.id);
284
285
// Create sample data
286
await Product.createEach([
287
{ name: 'Product 1', price: 29.99 },
288
{ name: 'Product 2', price: 39.99 }
289
]);
290
291
console.log('Bootstrap complete!');
292
}
293
};
294
```
295
296
### test
297
298
Run test suite (alias for `sails run test`):
299
300
```bash { .api }
301
sails test [options]
302
```
303
304
**Example**:
305
```bash
306
# Run all tests
307
sails test
308
309
# Run specific test file
310
sails test --grep "User model"
311
```
312
313
**Test Configuration** (`scripts/test.js`):
314
```javascript
315
module.exports = {
316
fn: async function() {
317
const { execSync } = require('child_process');
318
319
try {
320
// Run Mocha tests
321
execSync('npm test', { stdio: 'inherit' });
322
323
console.log('All tests passed!');
324
} catch (err) {
325
console.error('Tests failed:', err.message);
326
process.exit(1);
327
}
328
}
329
};
330
```
331
332
### lint
333
334
Run code linting (alias for `sails run lint`):
335
336
```bash { .api }
337
sails lint [options]
338
```
339
340
**Example**:
341
```bash
342
# Run linting
343
sails lint
344
345
# Fix auto-fixable issues
346
sails run lint --fix
347
```
348
349
### www
350
351
Compile assets to standalone www folder:
352
353
```bash { .api }
354
sails www [options]
355
```
356
357
**Purpose**: Generate static website from Sails app for CDN deployment
358
359
**Example**:
360
```bash
361
# Compile assets
362
sails www
363
364
# Output location: www/
365
# Contains: Static HTML, CSS, JS, images
366
```
367
368
### upgrade
369
370
Upgrade Sails application to newer version:
371
372
```bash { .api }
373
sails upgrade [options]
374
```
375
376
**Features**:
377
- Update package.json dependencies
378
- Migrate configuration files
379
- Update deprecated code patterns
380
- Backup original files
381
382
**Example**:
383
```bash
384
# Upgrade to latest Sails version
385
sails upgrade
386
387
# Check what would be upgraded (dry run)
388
sails upgrade --dry-run
389
```
390
391
### migrate
392
393
Run database migrations:
394
395
```bash { .api }
396
sails migrate [options]
397
```
398
399
**Examples**:
400
```bash
401
# Run pending migrations
402
sails migrate
403
404
# Create new migration
405
sails migrate create add-user-avatar
406
407
# Rollback last migration
408
sails migrate rollback
409
```
410
411
### deploy
412
413
Deploy application to cloud platforms:
414
415
```bash { .api }
416
sails deploy [target] [options]
417
```
418
419
**Supported Targets**:
420
- `heroku` - Deploy to Heroku
421
- `azure` - Deploy to Azure
422
- `aws` - Deploy to AWS
423
424
**Examples**:
425
```bash
426
# Deploy to Heroku
427
sails deploy heroku
428
429
# Deploy to specific environment
430
sails deploy heroku --environment staging
431
```
432
433
## Debug Commands
434
435
### debug-console
436
437
Start debug console session:
438
439
```bash { .api }
440
sails debug-console [options]
441
sails dc [options] # Alias
442
```
443
444
**Purpose**: Interactive debugging with inspector support
445
446
**Example**:
447
```bash
448
# Start debug console
449
sails debug-console
450
451
# Access in Chrome DevTools
452
# Set breakpoints in console code
453
```
454
455
## Information Commands
456
457
### version
458
459
Display version information:
460
461
```bash { .api }
462
sails version
463
sails --version
464
sails -v
465
sails -V
466
```
467
468
**Output Example**:
469
```
470
Sails v1.5.15
471
Node v18.17.0
472
NPM v9.6.7
473
```
474
475
### help
476
477
Display help information:
478
479
```bash { .api }
480
sails help [command]
481
sails --help
482
```
483
484
**Examples**:
485
```bash
486
# General help
487
sails help
488
489
# Command-specific help
490
sails help lift
491
sails help generate
492
sails help new
493
```
494
495
## Global Options
496
497
Most commands support these global options:
498
499
```bash { .api }
500
--silent # Suppress log output
501
--verbose # Enable verbose logging
502
--silly # Maximum logging detail
503
--prod # Production environment
504
--staging # Staging environment
505
--port [n] # Specify port number
506
```
507
508
## Configuration and Customization
509
510
### .sailsrc Configuration
511
512
Customize CLI behavior with `.sailsrc` file:
513
514
```json
515
{
516
"generators": {
517
"modules": {
518
"controller": "sails-generate-controller-api",
519
"model": "sails-generate-model-mongoose"
520
}
521
},
522
523
"commands": {
524
"lift": {
525
"environment": "development"
526
},
527
"generate": {
528
"templatesDirectory": "./custom-templates"
529
}
530
}
531
}
532
```
533
534
### Custom Generators
535
536
Create custom code generators:
537
538
```javascript
539
// generators/my-generator/index.js
540
module.exports = {
541
templatesDirectory: require('path').resolve(__dirname, './templates'),
542
543
targets: {
544
'./api/controllers/:filename': { template: 'controller.template' },
545
'./api/models/:filename': { template: 'model.template' }
546
},
547
548
before: function(scope, cb) {
549
scope.filename = scope.args[0] + 'Controller.js';
550
return cb();
551
}
552
};
553
```
554
555
**Usage**:
556
```bash
557
sails generate my-generator User
558
```
559
560
## Scripting and Automation
561
562
### Bash Scripting
563
564
```bash
565
#!/bin/bash
566
# deploy.sh
567
568
echo "Deploying application..."
569
570
# Run tests first
571
sails test
572
if [ $? -ne 0 ]; then
573
echo "Tests failed, aborting deployment"
574
exit 1
575
fi
576
577
# Build assets
578
sails www
579
580
# Deploy to production
581
sails deploy heroku --prod
582
583
echo "Deployment complete!"
584
```
585
586
### NPM Scripts Integration
587
588
```json
589
{
590
"scripts": {
591
"start": "sails lift",
592
"dev": "sails lift --verbose",
593
"test": "sails test",
594
"lint": "sails lint",
595
"build": "sails www",
596
"deploy": "./scripts/deploy.sh"
597
}
598
}
599
```
600
601
### Environment-Specific Commands
602
603
```bash
604
# Development
605
npm run dev
606
607
# Production
608
NODE_ENV=production npm start
609
610
# Testing
611
npm test
612
613
# Building
614
npm run build
615
```
616
617
The Sails.js CLI provides a comprehensive toolkit for efficient application development, testing, and deployment, with extensive customization options and integration capabilities for modern development workflows.