Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
fix: add handling for no output root
Browse files Browse the repository at this point in the history
Closes #60
  • Loading branch information
justinlettau committed Jan 16, 2019
1 parent 14125e2 commit 1b80a3b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default class Push {

directories.forEach(dir => {
if (dir) {
const files = glob.sync(`${config.output.root}/${dir}/**/*.sql`);
const files = glob.sync(`${config.getRoot()}/${dir}/**/*.sql`);
output.push(...files);
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/common/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Cache implements ICache {
}

try {
const file = path.join(this.config.output.root, Cache.defaultCacheFile);
const file = path.join(this.config.getRoot(), Cache.defaultCacheFile);
const cache: ICache = fs.readJsonSync(file);

this.files = cache.files;
Expand Down Expand Up @@ -84,7 +84,7 @@ export default class Cache implements ICache {
* Write a config file with provided configuration.
*/
write() {
const file = path.join(this.config.output.root, Cache.defaultCacheFile);
const file = path.join(this.config.getRoot(), Cache.defaultCacheFile);
const content: ICache = { files: this.files };

fs.writeJson(file, content, { spaces: 2 });
Expand All @@ -94,7 +94,7 @@ export default class Cache implements ICache {
* Check if default cache file exists.
*/
private doesDefaultExist() {
const file = path.join(this.config.output.root, Cache.defaultCacheFile);
const file = path.join(this.config.getRoot(), Cache.defaultCacheFile);

return fs.existsSync(file);
}
Expand Down
13 changes: 13 additions & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ export default class Config implements IConfig {
views: 'if-exists-drop'
};

/**
* Get root output directory.
*/
getRoot() {
let root = this.output.root;

if (!root || root === '.') {
root = './';
}

return root;
}

/**
* Get a connection by name, or the first available if `name` is not provided.
*
Expand Down
8 changes: 5 additions & 3 deletions src/common/file-utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class FileUtility {
return;
}

file = path.join(this.config.output.root, dir, file);
file = path.join(this.config.getRoot(), dir, file);
content = content.trim();

const cacheKey = this.normalize(file);
Expand Down Expand Up @@ -160,7 +160,9 @@ export default class FileUtility {
* @param file File path to normalize.
*/
private normalize(file: string) {
if (this.config.output.root.startsWith('./') && !file.startsWith('./')) {
const root = this.config.getRoot();

if (root.startsWith('./') && !file.startsWith('./')) {
file = `./${file}`;
}

Expand All @@ -171,7 +173,7 @@ export default class FileUtility {
* Load existing files and cache for comparison.
*/
private load() {
this.existingFiles = glob.sync(`${this.config.output.root}/**/*.sql`);
this.existingFiles = glob.sync(`${this.config.getRoot()}/**/*.sql`);
this.existingCache.load();
}
}

0 comments on commit 1b80a3b

Please sign in to comment.