Skip to content

Commit

Permalink
refactor: backslashes on Windows (#5457)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang authored Apr 9, 2024
1 parent 94f6ad3 commit 80dafe2
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 41 deletions.
15 changes: 8 additions & 7 deletions lib/box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Box extends EventEmitter {
const src = join(this.base, path);

return Cache.compareFile(
escapeBackslash(src.substring(ctx.base_dir.length)),
src.substring(ctx.base_dir.length),
() => getHash(src),
() => stat(src)
).then(result => ({
Expand All @@ -129,7 +129,7 @@ class Box extends EventEmitter {
if (!stats.isDirectory()) return;

// Check existing files in cache
const relativeBase = escapeBackslash(base.substring(ctx.base_dir.length));
const relativeBase = base.substring(ctx.base_dir.length);
const cacheFiles = Cache.filter(item => item._id.startsWith(relativeBase)).map(item => item._id.substring(relativeBase.length));

// Handle deleted files
Expand All @@ -156,11 +156,12 @@ class Box extends EventEmitter {
});

return BlueBirdPromise.reduce(this.processors, (count, processor) => {
const params = processor.pattern.match(path);
// patten supports *nix style path only, replace backslashes on Windows
const params = processor.pattern.match(escapeBackslash(path));
if (!params) return count;

const file = new File({
// source is used for filesystem path, keep backslashes on Windows
// source is used for file system path, keep backslashes on Windows
source: join(base, path),
// path is used for URL path, replace backslashes on Windows
path: escapeBackslash(path),
Expand Down Expand Up @@ -194,7 +195,7 @@ class Box extends EventEmitter {
const { base } = this;

function getPath(path) {
return escapeBackslash(path.substring(base.length));
return path.substring(base.length);
}

return this.process().then(() => watch(base, this.options)).then(watcher => {
Expand All @@ -214,7 +215,7 @@ class Box extends EventEmitter {

watcher.on('addDir', path => {
let prefix = getPath(path);
if (prefix) prefix += '/';
if (prefix) prefix += sep;

this._readDir(path, prefix);
});
Expand Down Expand Up @@ -287,7 +288,7 @@ function readDirWalker(ctx: Hexo, base: string, results: any[], ignore: any, pre
const prefixPath = `${prefix}${path}`;
if (stats) {
if (stats.isDirectory()) {
return readDirWalker(ctx, fullpath, results, ignore, `${prefixPath}/`);
return readDirWalker(ctx, fullpath, results, ignore, prefixPath + sep);
}
if (!isIgnoreMatch(fullpath, ignore)) {
results.push(prefixPath);
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/processor/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function processPage(ctx: Hexo, file: _File) {
}

function processAsset(ctx: Hexo, file: _File) {
const id = relative(ctx.base_dir, file.source).replace(/\\/g, '/');
const id = relative(ctx.base_dir, file.source);
const Asset = ctx.model('Asset');
const doc = Asset.findById(id);

Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/processor/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function scanAssetDir(ctx: Hexo, post) {
if (err && err.code === 'ENOENT') return [];
throw err;
}).filter(item => !isExcludedFile(item, ctx.config)).map(item => {
const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/');
const id = join(assetDir, item).substring(baseDirLength);
const renderablePath = id.substring(sourceDirLength + 1);
const asset = PostAsset.findById(id);

Expand Down Expand Up @@ -267,7 +267,7 @@ function shouldSkipAsset(ctx: Hexo, post, asset) {
function processAsset(ctx: Hexo, file: _File) {
const PostAsset = ctx.model('PostAsset');
const Post = ctx.model('Post');
const id = file.source.substring(ctx.base_dir.length).replace(/\\/g, '/');
const id = file.source.substring(ctx.base_dir.length);
const doc = PostAsset.findById(id);

if (file.type === 'delete') {
Expand Down
2 changes: 1 addition & 1 deletion lib/theme/processors/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { _File } from '../../box';

function process(file: _File) {
const Asset = this.model('Asset');
const id = file.source.substring(this.base_dir.length).replace(/\\/g, '/');
const id = file.source.substring(this.base_dir.length);
const { path } = file.params;
const doc = Asset.findById(id);

Expand Down
11 changes: 6 additions & 5 deletions test/scripts/box/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = 'test/' + name;
const cacheId = join('test/', name);

const processor = spy();
box.addProcessor(processor);
Expand All @@ -144,7 +144,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = 'test/' + name;
const cacheId = join('test/', name);

const processor = spy();
box.addProcessor(processor);
Expand All @@ -167,7 +167,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = 'test/' + name;
const cacheId = join('test/', name);

const processor = spy();
box.addProcessor(processor);
Expand All @@ -190,7 +190,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = 'test/' + name;
const cacheId = join('test/', name);

const processor = spy();
box.addProcessor(processor);
Expand All @@ -211,7 +211,8 @@ describe('Box', () => {

it('process() - delete', async () => {
const box = newBox('test');
const cacheId = 'test/a.txt';
// join will replace backslashes on Windows
const cacheId = join('test/', 'a.txt');

const processor = spy();
box.addProcessor(processor);
Expand Down
12 changes: 6 additions & 6 deletions test/scripts/processors/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('asset', () => {

await writeFile(file.source, 'foo');
await process(file);
const id = 'source/' + file.path;
const id = join('source/', file.path);
const asset = Asset.findById(id);

asset._id.should.eql(id);
Expand All @@ -103,7 +103,7 @@ describe('asset', () => {

await writeFile(file.source, 'foo');
await process(file);
const id = '../source/foo.jpg'; // The id should a relative path,because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly.
const id = join('../source/', 'foo.jpg'); // The id should a relative path, because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly.
const asset = Asset.findById(id);
asset._id.should.eql(id);
asset.path.should.eql(file.path);
Expand All @@ -122,7 +122,7 @@ describe('asset', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

await Promise.all([
writeFile(file.source, 'test'),
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('asset', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

await Promise.all([
writeFile(file.source, 'test'),
Expand All @@ -179,7 +179,7 @@ describe('asset', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

await Asset.insert({
_id: id,
Expand All @@ -197,7 +197,7 @@ describe('asset', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);
await process(file);

should.not.exist(Asset.findById(id));
Expand Down
31 changes: 17 additions & 14 deletions test/scripts/processors/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('post', () => {
});

await process(file);
const id = 'source/' + file.path;
const id = join('source/', file.path);
should.not.exist(PostAsset.findById(id));
});

Expand All @@ -141,7 +141,7 @@ describe('post', () => {
const postId = doc._id;
await process(file);

const id = 'source/' + file.path;
const id = join('source/', file.path);
const asset = PostAsset.findById(id);

asset._id.should.eql(id);
Expand All @@ -165,7 +165,7 @@ describe('post', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

const post = await Post.insert({
source: '_posts/foo.html',
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('post', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

const post = await Post.insert({
source: '_posts/foo.html',
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('post', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

const post = await Post.insert({
source: '_posts/foo.html',
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('post', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

const post = await Post.insert({
source: '_posts/foo.html',
Expand All @@ -290,7 +290,7 @@ describe('post', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

await writeFile(file.source, 'test');
await process(file);
Expand All @@ -309,7 +309,7 @@ describe('post', () => {
renderable: false
});

const id = 'source/' + file.path;
const id = join('source/', file.path);

await Promise.all([
writeFile(file.source, 'test'),
Expand Down Expand Up @@ -966,7 +966,7 @@ describe('post', () => {
'_fizz.jpg',
'_buzz.jpg'
].map(filename => {
const id = `source/_posts/foo/${filename}`;
const id = join('source/_posts/foo/', filename);
const path = join(hexo.base_dir, id);
const contents = filename.replace(/\.\w+$/, '');
return {
Expand Down Expand Up @@ -1022,7 +1022,8 @@ describe('post', () => {
renderable: true
});

const assetId = 'source/_posts/foo/bar.jpg';
// join will replace backslashes on Windows
const assetId = join('source/_posts/foo/', 'bar.jpg');
const assetPath = join(hexo.base_dir, assetId);

await Promise.all([
Expand Down Expand Up @@ -1068,7 +1069,8 @@ describe('post', () => {
renderable: true
});

const assetId = 'source/_posts/foo/bar.jpg';
// join will replace backslashes on Windows
const assetId = join('source/_posts/foo/', 'bar.jpg');
const assetPath = join(hexo.base_dir, assetId);

await Promise.all([
Expand Down Expand Up @@ -1106,7 +1108,8 @@ describe('post', () => {
renderable: true
});

const assetId = 'source/_posts/foo/bar.jpg';
// join will replace backslashes on Windows
const assetId = join('source/_posts/foo/', 'bar.jpg');
const assetPath = join(hexo.base_dir, assetId);

await Promise.all([
Expand Down Expand Up @@ -1283,7 +1286,7 @@ describe('post', () => {
writeFile(assetFile.source, 'test')
]);
await process(file);
const id = 'source/' + assetFile.path;
const id = join('source/', assetFile.path);
const post = Post.findOne({ source: file.path });
PostAsset.findById(id).renderable.should.be.true;

Expand Down Expand Up @@ -1319,7 +1322,7 @@ describe('post', () => {
writeFile(assetFile.source, 'test')
]);
await process(file);
const id = 'source/' + assetFile.path;
const id = join('source/', assetFile.path);
const post = Post.findOne({ source: file.path });
PostAsset.findById(id).renderable.should.be.false;

Expand Down
10 changes: 5 additions & 5 deletions test/scripts/theme_processors/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('source', () => {
type: 'create'
});

const id = 'themes/test/' + file.path;
const id = join('themes/test/', file.path);

await writeFile(file.source, 'test');
await process(file);
Expand All @@ -77,7 +77,7 @@ describe('source', () => {
type: 'update'
});

const id = 'themes/test/' + file.path;
const id = join('themes/test/', file.path);

await Promise.all([
writeFile(file.source, 'test'),
Expand All @@ -104,7 +104,7 @@ describe('source', () => {
type: 'skip'
});

const id = 'themes/test/' + file.path;
const id = join('themes/test/', file.path);

await Promise.all([
writeFile(file.source, 'test'),
Expand All @@ -130,7 +130,7 @@ describe('source', () => {
type: 'delete'
});

const id = 'themes/test/' + file.path;
const id = join('themes/test/', file.path);

await Asset.insert({
_id: id,
Expand All @@ -146,7 +146,7 @@ describe('source', () => {
type: 'delete'
});

const id = 'themes/test/' + file.path;
const id = join('themes/test/', file.path);

await process(file);
should.not.exist(Asset.findById(id));
Expand Down

0 comments on commit 80dafe2

Please sign in to comment.