Skip to content

Commit 0e81d35

Browse files
committed
-x flag runs a replace against all files if a directory is given, not just html files
1 parent 7787c08 commit 0e81d35

4 files changed

Lines changed: 46 additions & 46 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ npm install -g quickbase-cli
1111
```
1212

1313
## Usage
14-
quickbase-cli can be used for basic QuickBase code page development. It's possible to use quickbase-cli with modern SPA cli tools (angular cli, create-react-app, vue cli, etc.), but there are some pretty significant caveats. Check out the `qb deploy` command options (`-x` specifically) for details.
14+
quickbase-cli can be used for basic QuickBase code page development. It's probably possible to use quickbase-cli with modern SPA cli tools (angular cli, create-react-app, vue cli, etc.), but I haven't actually tried it so let me know how it goes.
1515

1616
There are three commands available for quickbase-cli:
1717
- qb init
@@ -72,8 +72,8 @@ This will upload the file(s) at `<file path or directory>` to the QuickBase appl
7272
**THAT WAS AN IMPORTANT FACT, PAY ATTENTION WHEN RUNNING `qb deploy` -- DON'T UPLOAD YOUR NODE_MODULES DIRECTORY TO QUICKBASE...**
7373

7474
There are two optional flags that can be passed to `qb deploy`. You can use them individually or multiple at a time:
75-
- `-w` (or `--watch`): watch for changes to `<file path or directory>` and deploy to QuickBase on change. After the initial deploy only the file that changes will be uploaded to QuickBase.
76-
- `-x` (or `--replace`): If you pass a directory to `qb deploy` then all .html files will run through a regex to replace asset file includes (i.e. `<script src="bundle.js"></script>` and/or `<link href="bundle.css"/>`) with their new QuickBase urls (`<script src="realm.quickbase.com/db/dbayemay?a=dbpage&pageID=123"></script>`). **Only .html files will have asset paths replaced, which means any asset paths inside your javascript (including React `<img />` src attributes) won't be replaced.** This is a good chunk of effort to implement efficiently, so I'll leave that as a potential path to greatness for anyone willing to put in the effort (PRs are always welcome).
75+
- `-w` (or `--watch`): watch for changes to `<file path or directory>` and deploy to QuickBase on change. After the initial deploy only the file that changes will be uploaded to QuickBase unless the `-x` flag is also passed, in which case the entire `<file path or directory>` source will be uploaded.
76+
- `-x` (or `--replace`): If you pass a directory to `qb deploy` then all files will run through a regex to replace asset file includes (i.e. `<script src="bundle.js"></script>`, `<link href="bundle.css"/>`, etc.) with their new QuickBase urls (`<script src="realm.quickbase.com/db/dbayemay?a=dbpage&pageID=123"></script>`). This is in no way an optimized command, so I'd avoid running it on YUGE directories.
7777

7878
### qb new (mostly useless)
7979
```bash
@@ -94,4 +94,4 @@ For now this is only a wrapper around `git clone`. After you pull down a repo yo
9494

9595
* ~~Moves are being made to add cool shit like a build process, global defaults, awesome starter templates, and pulling down existing code files from QuickBase. They're not out yet, so for now you're on your own.~~
9696

97-
* I no longer work with QuickBase applications, so the cool shit I had planned won't happen unless someone submits some dope ass pull requests.
97+
* I no longer work with QuickBase applications, so the cool shit I had planned won't happen unless someone submits some dope pull requests.

bin/qb-deploy.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ const fs = require('fs');
77
const path = require('path');
88
const program = require('commander');
99
const watch = require('chokidar').watch;
10+
const util = require('util');
1011

1112
const ApiClient = require('../lib/api');
1213
const { getFiles } = require('../lib/get-files');
1314

15+
const readFile = util.promisify(fs.readFile);
16+
const stat = util.promisify(fs.stat);
17+
1418
program
1519
.option('-w, --watch', 'deploy files on change')
1620
.option(
@@ -30,13 +34,18 @@ if (program.watch) {
3034
console.log(`Watching for file changes in ${sourceArg}`);
3135

3236
watch(sourceArg, {}).on('change', fileName => {
33-
console.log(`\nChange detected in ${fileName}. Deploying...`);
34-
qbDeploy(fileName);
37+
console.log(`\nChange detected in ${fileName}`);
38+
program.replace
39+
? qbDeploy(sourceArg)
40+
: qbDeploy(fileName);
3541
});
3642
}
3743

38-
function qbDeploy(source) {
39-
let isFile = fs.statSync(source).isFile();
44+
async function qbDeploy(source) {
45+
console.log('Uploading files to QuickBase...');
46+
47+
const stats = await fs.statSync(source);
48+
const isFile = stats.isFile();
4049

4150
if (isFile) {
4251
return uploadToQuickbase(source)
@@ -48,24 +57,14 @@ function qbDeploy(source) {
4857

4958
if (!isFile) {
5059
getFiles(source).then(files => {
51-
const { htmlFiles, assetFiles } = files;
52-
53-
const uploadHtmlFiles = program.replace
54-
? replaceUrlsAndUpload(htmlFiles, assetFiles)
55-
: htmlFiles.map(htmlFile => uploadToQuickbase(htmlFile));
56-
57-
const uploadAssetFiles = assetFiles.map(assetFile =>
58-
uploadToQuickbase(assetFile)
59-
);
60+
const uploadPromises = program.replace
61+
? files.map(file => replaceUrlsAndUpload(file, files))
62+
: files.map(file => uploadToQuickbase(file));
6063

61-
const uploadAllFiles = uploadHtmlFiles.concat(uploadAssetFiles);
62-
63-
return Promise.all(uploadAllFiles)
64+
return Promise.all(uploadPromises)
6465
.then(res =>
6566
console.log(
66-
`Successfully uploaded to QuickBase:\n=> ${htmlFiles
67-
.concat(assetFiles)
68-
.join('\n=> ')}`
67+
`Successfully uploaded to QuickBase:\n=> ${files.join('\n=> ')}`
6968
)
7069
)
7170
.catch(err => console.error(err));
@@ -90,23 +89,22 @@ function uploadToQuickbase(file, fileContents) {
9089
return api.uploadPage(codePageName, fileContents);
9190
}
9291

93-
function replaceUrlsAndUpload(htmlFiles, assetFiles) {
94-
return htmlFiles.map(htmlFile => {
95-
let htmlContent = fs.readFileSync(htmlFile, 'utf-8');
92+
async function replaceUrlsAndUpload(file, allFiles) {
93+
let fileContents = await readFile(file, 'utf-8');
9694

97-
assetFiles.forEach(assetFile => {
95+
allFiles.forEach(assetFile => {
96+
if (file !== assetFile) {
9897
const fileName = path.basename(assetFile);
9998
const regex = new RegExp(`("|')[^"]*${fileName}("|')`, 'g');
10099

101-
htmlContent = htmlContent.replace(
100+
fileContents = fileContents.replace(
102101
regex,
103102
generateCustomPageUrl(path.basename(assetFile))
104103
);
105-
});
106-
107-
console.log(htmlContent);
108-
return uploadToQuickbase(htmlFile, htmlContent);
104+
}
109105
});
106+
107+
return uploadToQuickbase(file, fileContents);
110108
}
111109

112110
function generateCustomPageUrl(fileName) {

lib/find-config.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
let fs = require('fs');
2-
let path = require('path');
1+
const fs = require('fs');
2+
const path = require('path');
33

44
const CONFIG_FILE_NAME = 'quickbase-cli.config.js';
55

6-
const findConfig = source => {
6+
const findConfig = (source, count = 1) => {
7+
if (count > 10) {
8+
console.log(
9+
'Couldn\'t find a "quickbase-cli.config.js" file. Make sure you\'ve run "qb init" in the root of your app.'
10+
);
11+
process.exit(1);
12+
}
13+
714
source = path.resolve(source);
815

9-
let isFile = fs.statSync(source).isFile();
16+
const isFile = fs.statSync(source).isFile();
1017
let children, parentDir;
1118

1219
if (isFile) {
@@ -17,12 +24,12 @@ const findConfig = source => {
1724
children = fs.readdirSync(source);
1825
}
1926

20-
let configFile = children.find(file => file == CONFIG_FILE_NAME);
27+
const configFile = children.find(file => file == CONFIG_FILE_NAME);
2128

2229
if (configFile) {
2330
return path.join(parentDir, CONFIG_FILE_NAME);
2431
} else {
25-
return findConfig(path.join(source, '..'));
32+
return findConfig(path.join(source, '..'), count + 1);
2633
}
2734
};
2835

lib/get-files.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ const readdir = util.promisify(fs.readdir);
66
const stat = util.promisify(fs.stat);
77

88
async function getFiles(source) {
9-
const files = {
10-
htmlFiles: [],
11-
assetFiles: []
12-
};
9+
const files = [];
1310

1411
await traverse(source, filePath => {
15-
if (path.extname(filePath) == '.html') {
16-
files.htmlFiles.push(filePath);
17-
} else if (path.basename(filePath) !== 'quickbase-cli.config.js') {
18-
files.assetFiles.push(filePath);
12+
if (path.basename(filePath) !== 'quickbase-cli.config.js') {
13+
files.push(filePath);
1914
}
2015
});
2116

0 commit comments

Comments
 (0)