Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit b83f1ff

Browse files
authored
Merge pull request #1 from uicrooks/dev
v0.2.0
2 parents cd69885 + c6ba132 commit b83f1ff

File tree

7 files changed

+1318
-22
lines changed

7 files changed

+1318
-22
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,43 @@
1414
<h2 align="center">Shopify Theme Lab CLI</h2>
1515

1616
Command Line Interface for [Shopify Theme Lab](https://github.com/uicrooks/shopify-theme-lab).
17+
> There is no need to install this package locally, `npx` takes care of everything, simply run the commands in your terminal.
1718
<!-- title / description (end) -->
1819
20+
<!-- system requirements (start) -->
21+
## System requirements
22+
23+
- Node.js >= `12.0.0`
24+
<!-- system requirements (end) -->
25+
1926
<!-- commands (start) -->
2027
## Commands
28+
> For all options you can also use the shorthand version, which is the first letter only, with a single dash in front e.g. For `--password` it's `-p`.
2129
22-
Create a local development environment
30+
**Create a local development environment:**
2331
```sh
2432
$ npx themelab create <directory-name>
2533
```
34+
35+
**Create a local development environment from a git repo:**
36+
```sh
37+
$ npx themelab create <directory-name> --repo https://github.com/user/repo
38+
```
39+
40+
**Initialize a remote Shopify theme and create a local config file:**
41+
> Run in the root directory of your Shopify Theme Lab project!
42+
```sh
43+
$ npx themelab shopify:init --password [your-api-password] --store [your-store.myshopify.com] --env [dev or live] --name ['theme name']
44+
```
45+
46+
**Create a local config file (connect to an existing theme on a remote store):**
47+
> Run in the root directory of your Shopify Theme Lab project!
48+
```sh
49+
$ npx themelab shopify:init --password [your-api-password] --store [your-store.myshopify.com] --env [dev or live] --id [theme-id]
50+
```
51+
52+
**Display help:**
53+
```sh
54+
$ npx themelab --help
55+
```
2656
<!-- commands (end) -->

bin/themelab.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,31 @@ const { program } = require('commander')
99
* setup
1010
*/
1111
program
12+
.storeOptionsAsProperties(false)
1213
.version(`Shopify Theme Lab CLI ${require('../package').version}`, '-v, --version', 'output the version number')
1314
.usage('<command> [options]')
1415

1516
/**
1617
* commands
1718
*/
1819
program
19-
.command('create <project-name>')
20-
.description('initialize a Shopify Theme Lab project')
21-
.action((projectName) => {
22-
require('../lib/create')(projectName)
20+
.command('create <directory>')
21+
.description('create a Shopify Theme Lab environment')
22+
.option('-r, --repo <url>', 'use a git repository url')
23+
.action((directory, cmd) => {
24+
require('../lib/create')(directory, cmd)
25+
})
26+
27+
program
28+
.command('shopify:init')
29+
.description('initialize theme on remote Shopify store and a local config')
30+
.requiredOption('-p, --password <password>', 'Shopify plugin API password')
31+
.requiredOption('-s, --store <store>', '[your-store].myshopify.com')
32+
.option('-e, --env <env>', 'Environment [dev] or [live]', 'dev')
33+
.option('-n, --name <name>', 'Theme name', 'Shopify Theme Lab')
34+
.option('-i, --id <id>', 'If you provide a theme id, the command only creates a local config')
35+
.action((cmd) => {
36+
require('../lib/shopify-init')(cmd)
2337
})
2438

2539
/**

lib/create.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ const clone = require('git-clone')
55
const spinner = require('./utils/spinner')
66
const config = require('./config')
77

8-
module.exports = async (directory) => {
9-
const currentDirectory = process.cwd()
10-
const targetDirectory = directory.replace(/\s/, '-')
11-
const destinationPath = path.resolve(__dirname, `${currentDirectory}/${targetDirectory}`)
8+
module.exports = async (directory, cmd) => {
9+
spinner.start()
1210

13-
if (fs.existsSync(destinationPath)) {
14-
console.error(chalk.red('directory already exists'))
15-
process.exit()
16-
}
11+
const currentDirectory = process.cwd()
12+
const targetDirectory = path.resolve(__dirname, `${currentDirectory}/${directory}`)
13+
const repo = cmd.opts().repo || config.repo.env
1714

18-
await clone(config.repo.env, destinationPath, {} , async () => {
19-
spinner.start()
15+
clone(repo, targetDirectory, {} , async (e) => {
16+
if (e) {
17+
spinner.fail(chalk`{red Error: ${e}}`)
18+
process.exit()
19+
}
2020

21-
await fs.remove(path.resolve(destinationPath, '.git')) // remove .git directory
21+
// remove .git directory
22+
await fs.remove(path.resolve(targetDirectory, '.git'))
2223

23-
console.log(chalk.green(`✓ Shopify Theme Lab project successfully created in ${targetDirectory} directory`))
24-
spinner.stop()
24+
spinner.succeed(chalk`{green successfully created Shopify Theme Lab environment}`)
2525
})
2626
}

lib/shopify-init.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const path = require('path')
2+
const fs = require('fs-extra')
3+
const chalk = require('chalk')
4+
const yaml = require('yaml')
5+
const axios = require('axios')
6+
const themeKit = require('@shopify/themekit')
7+
8+
module.exports = async (cmd) => {
9+
/**
10+
* variables
11+
*/
12+
const options = cmd.opts()
13+
const currentDirectory = process.cwd()
14+
const configPath = path.resolve(__dirname, `${currentDirectory}/.config/shopify/shopify.${options.env}.yml`)
15+
let themeId = options.id || null
16+
17+
/**
18+
* check env value
19+
*/
20+
if (!options.env.match(/^(dev|live)$/)) {
21+
console.error(chalk`{red Error: env should be {inverse dev} or {inverse live}}`)
22+
process.exit()
23+
}
24+
25+
/**
26+
* initialize local config and a remote Shopify theme
27+
*/
28+
const initTheme = async () => {
29+
// initialize empty theme on shopify store
30+
if (!options.id) {
31+
try {
32+
const response = await axios.post(
33+
`https://${options.store}/admin/api/2020-10/themes.json`,
34+
{ theme: { name: options.name } },
35+
{ headers: { 'X-Shopify-Access-Token': options.password } }
36+
)
37+
38+
// assign theme id
39+
themeId = response.data.theme.id.toString()
40+
} catch (e) {
41+
console.error(chalk`{red Error: ${e}}`)
42+
process.exit()
43+
}
44+
}
45+
46+
// create yaml config
47+
const yamlConfig = yaml.stringify({
48+
[options.env]: {
49+
password: options.password,
50+
theme_id: themeId,
51+
store: options.store,
52+
directory: 'shopify',
53+
ignores: [
54+
'.config/shopify/.shopifyignore'
55+
]
56+
}
57+
})
58+
59+
// write Shopify config file
60+
try {
61+
await fs.outputFile(configPath, yamlConfig)
62+
} catch (e) {
63+
console.error(chalk`{red Error: ${e}}`)
64+
process.exit()
65+
}
66+
67+
if (!options.id) {
68+
// write settings_data.json to shopify/config
69+
try {
70+
const settingsData = {
71+
current: 'Default',
72+
presets: {
73+
Default: {
74+
sections: {
75+
'dynamic-section': {
76+
type: 'dynamic-section',
77+
category: 'Text'
78+
}
79+
},
80+
content_for_index: [
81+
'dynamic-section'
82+
]
83+
}
84+
}
85+
}
86+
87+
const settingsFilePath = path.resolve(__dirname, `${currentDirectory}/shopify/config/settings_data.json`)
88+
89+
// check if settings_data.json already exists and if not then create that file
90+
if (!fs.existsSync(settingsFilePath)) {
91+
await fs.outputFile(settingsFilePath, JSON.stringify(settingsData, null, 2))
92+
}
93+
} catch (e) {
94+
console.error(chalk`{red Error: ${e}}`)
95+
process.exit()
96+
}
97+
98+
// upload Shopify theme to remote
99+
try {
100+
await themeKit.command('deploy', {
101+
config: configPath,
102+
env: options.env
103+
})
104+
} catch (e) {
105+
console.error(chalk`{red Error: ${e}}`)
106+
process.exit()
107+
}
108+
}
109+
110+
console.log(chalk`{green ✓ theme successfully initialized}`)
111+
}
112+
113+
/**
114+
* initialize theme
115+
*/
116+
initTheme()
117+
}

lib/utils/spinner.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const ora = require('ora')
1010
* start: spinner.start('text')
1111
* stop: spinner.stop()
1212
*/
13-
const spinner = ora()
13+
const spinner = ora({
14+
color: 'yellow'
15+
})
1416

1517
/**
1618
* export

0 commit comments

Comments
 (0)