Skip to content

Commit

Permalink
feat: handles absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 12, 2019
1 parent 53589c3 commit 7daef30
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 49 deletions.
10 changes: 10 additions & 0 deletions src/get-schema/absolute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from 'path';

export default function absolute(options: {
file: string;
cwd: string;
}): string {
return path.isAbsolute(options.file)
? options.file
: path.join(options.cwd, options.file);
}
4 changes: 2 additions & 2 deletions src/get-schema/create-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export default function createTree(
const obj: any = {};
getFiles([file], directory, options, recursive).forEach((item) => {
const content = read(
path.join(directory, item.dir, item.file),
path.join(item.cwd, item.directory, item.name),
options,
schemas
);
// Get keys
let keys = item.file.split(path.sep);
let keys = path.join(item.directory, item.name).split(path.sep);
keys[keys.length - 1] = path.basename(
keys[keys.length - 1],
path.extname(keys[keys.length - 1])
Expand Down
8 changes: 4 additions & 4 deletions src/get-schema/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export default function fetch(
schemas: yaml.Schema[]
): any[] {
const paths = Array.isArray(payload.paths) ? payload.paths : [payload.paths];
const files = getFiles(paths, directory, options, payload.recursive).map(
(item) => path.join(item.dir, item.file)
);
const files = getFiles(paths, directory, options, payload.recursive);

return files
.map((x) => read(path.join(directory, x), options, schemas))
.map((file) =>
read(path.join(file.cwd, file.directory, file.name), options, schemas)
)
.concat(payload.data || []);
}
24 changes: 12 additions & 12 deletions src/get-schema/get-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ import path from 'path';
import fs from 'fs';
import recursivedir from 'fs-readdir-recursive';
import { IOptions, IFileDefinition } from '~/types';
import absolute from './absolute';

export default function getFiles(
paths: string[],
directory: string,
cwd: string,
options: IOptions,
recursive?: boolean
): IFileDefinition[] {
return paths.reduce((acc: IFileDefinition[], file: string) => {
const stat = fs.statSync(path.join(directory, file));
file = absolute({ file, cwd });
const stat = fs.statSync(file);
return stat.isDirectory()
? acc.concat(
getFromDir(path.join(directory, file), options, recursive).map(
(item) => ({
cwd: directory,
dir: file,
file: item
})
)
getFromDir(file, options, recursive).map((item) => ({
cwd: file,
directory: path.dirname(item),
name: path.basename(item)
}))
)
: acc.concat({
cwd: directory,
dir: path.dirname(file),
file: path.basename(file)
cwd,
directory: '.',
name: path.relative(cwd, file)
});
}, []);
}
Expand Down
39 changes: 12 additions & 27 deletions src/get-schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import yaml from 'js-yaml';
import path from 'path';
import { IOptions, IPayload } from '~/types';
import read from '~/read';
import fetch from './fetch';
import merge from './merge';
import createTree from './create-tree';
import validatePayload from './validate-payload';
import absolute from './absolute';

export default function getSchema(
directory: string,
cwd: string,
options?: IOptions | null,
schemas: yaml.Schema[] = [yaml.DEFAULT_SAFE_SCHEMA]
): yaml.Schema {
Expand All @@ -17,11 +17,11 @@ export default function getSchema(
const types = [
new yaml.Type('tag:yaml.org,2002:import/single', {
kind: 'scalar',
resolve(data) {
return typeof data === 'string';
resolve(file) {
return typeof file === 'string';
},
construct(data) {
return read(path.join(directory, data), opts, schemas);
construct(file) {
return read(absolute({ file, cwd }), opts, schemas);
}
}),
new yaml.Type('tag:yaml.org,2002:import/sequence', {
Expand All @@ -31,10 +31,7 @@ export default function getSchema(
},
construct(files) {
const payload: IPayload = { paths: files, strategy: 'sequence' };
return merge(
fetch(payload, directory, opts, schemas),
payload.strategy
);
return merge(fetch(payload, cwd, opts, schemas), payload.strategy);
}
}),
new yaml.Type('tag:yaml.org,2002:import/shallow', {
Expand All @@ -44,10 +41,7 @@ export default function getSchema(
},
construct(files) {
const payload: IPayload = { paths: files, strategy: 'shallow' };
return merge(
fetch(payload, directory, opts, schemas),
payload.strategy
);
return merge(fetch(payload, cwd, opts, schemas), payload.strategy);
}
}),
new yaml.Type('tag:yaml.org,2002:import/merge', {
Expand All @@ -57,10 +51,7 @@ export default function getSchema(
},
construct(files) {
const payload: IPayload = { paths: files, strategy: 'merge' };
return merge(
fetch(payload, directory, opts, schemas),
payload.strategy
);
return merge(fetch(payload, cwd, opts, schemas), payload.strategy);
}
}),
new yaml.Type('tag:yaml.org,2002:import/deep', {
Expand All @@ -70,10 +61,7 @@ export default function getSchema(
},
construct(files) {
const payload: IPayload = { paths: files, strategy: 'deep' };
return merge(
fetch(payload, directory, opts, schemas),
payload.strategy
);
return merge(fetch(payload, cwd, opts, schemas), payload.strategy);
}
}),
new yaml.Type('tag:yaml.org,2002:import/payload', {
Expand All @@ -82,10 +70,7 @@ export default function getSchema(
return validatePayload(payload);
},
construct(payload: IPayload) {
return merge(
fetch(payload, directory, opts, schemas),
payload.strategy
);
return merge(fetch(payload, cwd, opts, schemas), payload.strategy);
}
}),
new yaml.Type('tag:yaml.org,2002:import/tree', {
Expand All @@ -99,7 +84,7 @@ export default function getSchema(
: [payload.paths];
const data = paths
.map((path) =>
createTree(path, directory, opts, schemas, payload.recursive)
createTree(path, cwd, opts, schemas, payload.recursive)
)
.concat(payload.data || []);
return merge(data, payload.strategy);
Expand Down
4 changes: 2 additions & 2 deletions src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export default function read(
options?: IOptions | null,
schemas?: yaml.Schema[]
): any {
const dir = path.dirname(input);
const cwd = path.dirname(input);
const src = fs.readFileSync(input, 'utf8');

const opts = Object.assign({ safe: true }, options);

return yaml[opts.safe ? 'safeLoad' : 'load'](src, {
...opts,
filename: input,
schema: getSchema(dir, opts, schemas)
schema: getSchema(cwd, opts, schemas)
});
}
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export interface IPayload {

export interface IFileDefinition {
cwd: string;
dir: string;
file: string;
directory: string;
name: string;
}

0 comments on commit 7daef30

Please sign in to comment.